+ -
当前位置:首页 → 问答吧 → 菜鸟求请教~这段程序编译老是出问题。。崩溃了

菜鸟求请教~这段程序编译老是出问题。。崩溃了

时间:2011-12-17

来源:互联网

#include "stdafx.h"
#include <iostream>
#include <string>
#include "winsock2.h"
#include "stdio.h" 
using namespace std; 

string getIp()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData); char HostName[128];
gethostname(HostName,128);
struct hostent* pHost = gethostbyname(HostName);
string ipstr = inet_ntoa(*(struct in_addr *)pHost-> h_addr_list[0]);
return ipstr;
WSACleanup();
}

int _tmain(int argc, _TCHAR* argv[])
{

cout << getIp() << endl;

system("pause");
return 0;
}

报错说
无法解析的外部符号 

WSAStartup gethostname gethostbyname inet_ntoa WSACleanup

为什么不能用这些函数了? 已经#include "winsock2.h"了啊

作者: ghetto   发布时间: 2011-12-17

引入库文件ws2_32.lib

作者: gold_water   发布时间: 2011-12-17

哈哈 搞定了 谢谢 太感谢了

作者: ghetto   发布时间: 2011-12-17

谢谢 就是这个问题!
引用 1 楼 gold_water 的回复:
引入库文件ws2_32.lib

作者: ghetto   发布时间: 2011-12-17

书上的例子?

作者: coolcoffee4051982   发布时间: 2011-12-17