C++ undefined reference to `__imp_WSACleanup‘解决方案

在进行Windows套接字编程时,遇到C++编译错误,提示缺少`__imp_WSAStartup`和`__imp_inet_addr`的引用。解决方法是在g++编译指令中添加`-lwsock32`选项,例如:`g++ src/im.cpp -o src/im.exe -lwsock32`,这使得程序能够正确链接到所需的Windows套接字库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在玩windows套接字时,遇到一个问题,估计是c++和c的winsocket有区别:
#pragma comment(lib, "wgs2_32")代码在运行时报错:
在这里插入图片描述

C:\Users\46304\AppData\Local\Temp\ccVgMc9v.o:im.cpp:(.text+0x70): undefined reference to `__imp_WSAStartup' 
C:\Users\46304\AppData\Local\Temp\ccVgMc9v.o:im.cpp:(.text+0xd5): undefined reference to `__imp_inet_addr'  
C:\Users\46304\AppData\Local\Temp\ccVgMc9v.o:im.cpp:(.text+0

参考网上的解决方案,在终端输入编译指令时需要加上这样一句话-lwsock32
即输入g++ src/im.cpp -o src/im.exe -lwsock32,就能正常编译成功

||=== 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值