[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

error LNK2001: unresolved external symbol __imp__gethostname@8

Posted on 2002-05-14
2
Medium Priority
?
1,354 Views
Last Modified: 2008-03-10
i write a simple program to get ip address.
code sample:
#include <windows.h>
#include "stdafx.h"
#include <winsock2.h>

int main(int argc, char* argv[])
{
     char *a = NULL;
     a = new char[255];
     gethostname(a,255);
     printf("%s",a);
     delete a;
    return 0;

}

when i compile the program occur following problem,

Deleting intermediate files and output files for project 'getSoftInf - Win32 Debug'.
--------------------Configuration: getSoftInf - Win32 Debug--------------------
Compiling...
StdAfx.cpp
Compiling...
getSoftInf.cpp
Linking...
getSoftInf.obj : error LNK2001: unresolved external symbol __imp__gethostname@8
Debug/getSoftInf.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

getSoftInf.exe - 2 error(s), 0 warning(s)

0
Comment
Question by:zhouweijian
2 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 300 total points
ID: 7008105
Hi ,

you'll have to link with Ws2_32.lib ... simply open 'Project->Settings' dialog, select 'Link'-tab
and add Ws2_32.lib to 'Object/library modules'.

hope that helps,

ZOPPO
0
 
LVL 86

Expert Comment

by:jkr
ID: 7008346
... and don't forget to call 'WSAStartup()':

WORD wVersionRequested;
WSADATA wsaData;
int err;
 
wVersionRequested = MAKEWORD( 2, 2 );
 
err = WSAStartup( wVersionRequested, &wsaData );


and  call 'WSACleanup()' at the end of your program...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question