clion使用Mingw编译器编译winsocket程序报链接错误undefined reference to `_imp__socket@12‘登

错误信息

CMakeFiles\client.dir/objects.a(client.cpp.obj): In function `Z14recvFromServerPv@4':
E:/Tencent/mutil_thread_ope/Client/client.cpp:24: undefined reference to `_imp__recv@16'
CMakeFiles\client.dir/objects.a(client.cpp.obj): In function `main':
E:/Tencent/mutil_thread_ope/Client/client.cpp:38: undefined reference to `_imp__WSAStartup@8'
E:/Tencent/mutil_thread_ope/Client/client.cpp:39: undefined reference to `_imp__socket@12'
E:/Tencent/mutil_thread_ope/Client/client.cpp:43: undefined reference to `_imp__inet_addr@4'
E:/Tencent/mutil_thread_ope/Client/client.cpp:44: undefined reference to `_imp__htons@4'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\client.dir\build.make:105: client.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:126: CMakeFiles/client.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:133: CMakeFiles/client.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:150: client] Error 2

在这里插入图片描述

今天在网上看到了C++,socket编程的代码,心血来潮想要试一下,复制到本地项目后运行报以上错误,

实验环境

clion 2021.1.3编译器
cmake版本3.17
Mingw编译器 8.1版本

根据报错显示是我们的链接错误,windows上的socket编程需要链接库ws2_32 wsock32

解决方法

在cmaklist.txt文件中加上

link_libraries(ws2_32 wsock32)

例如:我的cmakelist.txt文件

cmake_minimum_required(VERSION 3.17)
project(mutil_thread_ope)

set(CMAKE_CXX_STANDARD 14)
link_libraries(ws2_32 wsock32)

add_executable(mutil_thread_ope main.cpp Server/ope_server.cpp Server/ope_server.h Client/client.cpp)
add_executable(client Client/client.cpp)
add_executable(ope_server Server/ope_server.cpp)
  • 7
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
这个错误信息表明在编译过程中出现了对`__imp_socket`的未定义引用。根据引用\[1\]中的错误信息,这个错误可能是由于缺少对`socket`函数的链接导致的。引用\[2\]中的代码片段显示了在代码中手动加载了`ws2_32.lib`库文件,这是用于网络通信的库文件。因此,你需要确保在编译过程中正确链接了`ws2_32.lib`库文件。 另外,引用\[3\]中的CMakeLists.txt文件显示了在项目中添加了`ws2_32`和`wsock32`库文件的链接。你可以检查一下你的CMakeLists.txt文件,确保正确添加了这些库文件的链接。 综上所述,你需要确保在编译过程中正确链接了`ws2_32.lib`库文件,并检查一下你的CMakeLists.txt文件是否正确添加了`ws2_32`和`wsock32`库文件的链接。这样应该能够解决`undefined reference to __imp_socket`的问题。 #### 引用[.reference_title] - *1* *3* [clion使用Mingw编译器编译winsocket程序链接错误undefined reference to `_imp__socket@12](https://blog.csdn.net/weixin_44201089/article/details/119960288)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Clion使用Socketundefined reference to `__imp_WSAStartup‘(解决办法)](https://blog.csdn.net/qq_42282949/article/details/127659581)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值