Solved

error LNK2001: unresolved external symbol __imp__gethostname@8

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 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