Solved

error LNK2001: unresolved external symbol __imp__gethostname@8

Posted on 2002-05-14
2
1,298 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 100 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

828 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