C++报错集合:undefined reference to `htonl@4'

在Windows下 安装eclipse,MinGW,环境变量也配置完后,而且成功的运行了“hello world”程序。

结果自己写了个简单程序,里面调用了htonl()函数,也加入了头文件#include <winsock.h>,

结果碰到一个麻烦,就是编译出错,提示: undefined reference to `htonl@4'

一般说来,

undefined reference to ~~~~原因就是缺少什么什么库
如下:
在环境变变量中加入
PATH
C:\Program Files\MinGW\bin;
(如果系统已经有其它的C/C++编译器,请把C:\Program Files\MinGW\bin;加在最前面)

LIBRARY_PATH
C:\Program Files\MinGW\lib;

C_INCLUDE_PATH
C:\Program Files\MinGW\include;

CPLUS_INCLUDE_PATH
C:\Program Files\MinGW\include\c++\3.2.3;
C:\Program Files\MinGW\include\c++\3.2.3\mingw32;
C:\Program Files\MinGW\include\c++\3.2.3\backward;
C:\Program Files\MinGW\include;
不过这并不是问题关键
关键是无意中在编译后加上了-lwsock32
问题就解决了

 

具体加上-lwsock32的方法是:在project->properties->(左边)c/c++Build->(中间)GCC C Linker里边的Libraries->(右边)上边的写上wsock32

||=== Build: Debug 在 server 中 (编译器: GNU GCC Compiler) ===| D:\学习\计网\TCP\server\main.cpp|6|warning: ignoring #pragma comment [-Wunknown-pragmas]| D:\学习\计网\TCP\server\main.cpp||In function 'int main()':| D:\学习\计网\TCP\server\main.cpp|15|warning: unused variable 'sin_size' [-Wunused-variable]| obj\Debug\main.o||In function `main':| D:\学习\计网\TCP\server\main.cpp|18|undefined reference to `__imp_WSAStartup'| D:\学习\计网\TCP\server\main.cpp|24|undefined reference to `__imp_socket'| D:\学习\计网\TCP\server\main.cpp|26|undefined reference to `__imp_WSACleanup'| D:\学习\计网\TCP\server\main.cpp|34|undefined reference to `__imp_htonl'| D:\学习\计网\TCP\server\main.cpp|35|undefined reference to `__imp_htons'| D:\学习\计网\TCP\server\main.cpp|38|undefined reference to `__imp_bind'| D:\学习\计网\TCP\server\main.cpp|40|undefined reference to `__imp_closesocket'| D:\学习\计网\TCP\server\main.cpp|41|undefined reference to `__imp_WSACleanup'| D:\学习\计网\TCP\server\main.cpp|46|undefined reference to `__imp_listen'| D:\学习\计网\TCP\server\main.cpp|48|undefined reference to `__imp_closesocket'| D:\学习\计网\TCP\server\main.cpp|49|undefined reference to `__imp_WSACleanup'| D:\学习\计网\TCP\server\main.cpp|57|undefined reference to `__imp_accept'| D:\学习\计网\TCP\server\main.cpp|62|undefined reference to `__imp_inet_ntoa'| D:\学习\计网\TCP\server\main.cpp|65|undefined reference to `__imp_recv'| D:\学习\计网\TCP\server\main.cpp|75|undefined reference to `__imp_send'| D:\学习\计网\TCP\server\main.cpp|78|undefined reference to `__imp_closesocket'| ||error: ld returned 1 exit status| ||=== 构建 失败: 17 error(s), 2 warning(s) (0 分, 0 秒) ===| 出现以上问题,如何解决
05-19
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值