Solved

error LNK2001: unresolved external symbol __imp__gethostname@8

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

Accepted Solution

by:
Zoppo earned 100 total points
Comment Utility
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
Comment Utility
... 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now