错误信息
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)
|