winsock编译遇到的链接问题

近期由于项目要求,需要用到windows下的网络编程,由于以前自己总是在linux下开发,对windows平台不太熟悉,最近两天看了一些资料,写了个简单的服务器socket接收客户端请求,可是在编译时出现以下提示:

Linking...
IProxy.obj : error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function "void __cdecl StartWinsock(void)" (?StartWinsock@@YAXXZ)
IProxy.obj : error LNK2019: unresolved external symbol __imp__accept@12 referenced in function "void __cdecl StartProxyServer(void)" (?StartProxyServer@@YAXXZ)
IProxy.obj : error LNK2019: unresolved external symbol __imp__listen@8 referenced in function "void __cdecl StartProxyServer(void)" (?StartProxyServer@@YAXXZ)
IProxy.obj : error LNK2019: unresolved external symbol __imp__bind@12 referenced in function "void __cdecl StartProxyServer(void)" (?StartProxyServer@@YAXXZ)
IProxy.obj : error LNK2019: unresolved external symbol __imp__htons@4 referenced in function "void __cdecl StartProxyServer(void)" (?StartProxyServer@@YAXXZ)
IProxy.obj : error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function "void __cdecl StartProxyServer(void)" (?StartProxyServer@@YAXXZ)
IProxy.obj : error LNK2019: unresolved external symbol __imp__socket@12 referenced in function "void __cdecl StartProxyServer(void)" (?StartProxyServer@@YAXXZ)
IProxy.obj : error LNK2019: unresolved external symbol __imp__connect@12 referenced in function "void __cdecl StartProxyClient(void)" (?StartProxyClient@@YAXXZ)
IProxy.obj : error LNK2019: unresolved external symbol __imp__gethostbyname@4 referenced in function "void __cdecl StartProxyClient(void)" (?StartProxyClient@@YAXXZ)
IProxy.obj : error LNK2019: unresolved external symbol __imp__send@16 referenced in function "void __cdecl StartProxy(void)" (?StartProxy@@YAXXZ)
IProxy.obj : error LNK2019: unresolved external symbol __imp__recv@16 referenced in function "void __cdecl StartProxy(void)" (?StartProxy@@YAXXZ)
C:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\HProxy\Debug\HProxyDLL.dll : fatal error LNK1120: 11 unresolved externals


后来才知道,原因在于工程中未增加合适的静态库文件造成的,只需要在工程中增加以下代码,在编译过程中让静态库参与链接成可执行程序即可。

#pragma comment(lib, "ws2_32.lib")

你可能感兴趣的:(winsock编译遇到的链接问题)