Eclipse C/C++关于socket(tcp/ip)相关API编译错误的问题
MinGW支持winsock2,但到编译的时候总是报错:
undefined reference to `inet_addr@4'
undefined reference to `gethostbyname@4'
undefined reference to `WSAGetLastError@0'
undefined reference to `inet_ntoa@4'
undefined reference to `WSAStartup@8'
undefined reference to `inet_ntoa@4'
undefined reference to `socket@12'
undefined reference to `htons@4'
undefined reference to `bind@12'
undefined reference to `listen@8'
undefined reference to `accept@12'
undefined reference to `recv@16'
undefined reference to `send@16'
undefined reference to `shutdown@8'
undefined reference to `closesocket@4'
undefined reference to `closesocket@4'
undefined reference to `WSACleanup@0'
这错一看,要么是程序没有加载winsock2.h库,要么是程序make的时候出错。
检查console连接结果,发现确实是缺少socket的链接参数:-lws2_32.
在eclipse中添加make参数的方法:
右键工程 -> properties -> C++ build -> settings -> Tool Settings
在左边树形结构中选中 MinGW C++ Linker,此时在右边的command line pattern就是make时的参数设置,在后面加上-lws2_32就可以了。
再编译,成功了。
设置的截图如下:
[img]http://hi.csdn.net/attachment/201203/6/0_1331024887Y6H6.gif[/img]
MinGW支持winsock2,但到编译的时候总是报错:
undefined reference to `inet_addr@4'
undefined reference to `gethostbyname@4'
undefined reference to `WSAGetLastError@0'
undefined reference to `inet_ntoa@4'
undefined reference to `WSAStartup@8'
undefined reference to `inet_ntoa@4'
undefined reference to `socket@12'
undefined reference to `htons@4'
undefined reference to `bind@12'
undefined reference to `listen@8'
undefined reference to `accept@12'
undefined reference to `recv@16'
undefined reference to `send@16'
undefined reference to `shutdown@8'
undefined reference to `closesocket@4'
undefined reference to `closesocket@4'
undefined reference to `WSACleanup@0'
这错一看,要么是程序没有加载winsock2.h库,要么是程序make的时候出错。
检查console连接结果,发现确实是缺少socket的链接参数:-lws2_32.
在eclipse中添加make参数的方法:
右键工程 -> properties -> C++ build -> settings -> Tool Settings
在左边树形结构中选中 MinGW C++ Linker,此时在右边的command line pattern就是make时的参数设置,在后面加上-lws2_32就可以了。
再编译,成功了。
设置的截图如下:
[img]http://hi.csdn.net/attachment/201203/6/0_1331024887Y6H6.gif[/img]