初用CLion,在编写一个简单网络程序时,报了这个错

“C:\Program Files\JetBrains\CLion 2017.3.3\bin\cmake\bin\cmake.exe” –build C:\Users\foyou\CLionProjects\CL01\cmake-build-debug –target CL01 – -j 4
[ 50%] Linking CXX executable CL01.exe
CMakeFiles\CL01.dir/objects.a(main.cpp.obj): In function main':
C:/Users/foyou/CLionProjects/CL01/main.cpp:6: undefined reference to
WSAStartup@8’
C:/Users/foyou/CLionProjects/CL01/main.cpp:8: undefined reference to socket@12'
C:/Users/foyou/CLionProjects/CL01/main.cpp:13: undefined reference to
inet_addr@4’
C:/Users/foyou/CLionProjects/CL01/main.cpp:14: undefined reference to htons@4'
C:/Users/foyou/CLionProjects/CL01/main.cpp:15: undefined reference to
bind@12’
C:/Users/foyou/CLionProjects/CL01/main.cpp:17: undefined reference to listen@8'
C:/Users/foyou/CLionProjects/CL01/main.cpp:21: undefined reference to
accept@12’
C:/Users/foyou/CLionProjects/CL01/main.cpp:24: undefined reference to send@16'
C:/Users/foyou/CLionProjects/CL01/main.cpp:26: undefined reference to
closesocket@4’
C:/Users/foyou/CLionProjects/CL01/main.cpp:27: undefined reference to closesocket@4'
C:/Users/foyou/CLionProjects/CL01/main.cpp:29: undefined reference to
WSACleanup@0’
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: * [CL01.exe] Error 1
CMakeFiles\CL01.dir\build.make:95: recipe for target ‘CL01.exe’ failed
CMakeFiles\Makefile2:66: recipe for target ‘CMakeFiles/CL01.dir/all’ failed
mingw32-make.exe[2]: * [CMakeFiles/CL01.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target ‘CMakeFiles/CL01.dir/rule’ failed
mingw32-make.exe[1]: * [CMakeFiles/CL01.dir/rule] Error 2
Makefile:117: recipe for target ‘CL01’ failed
mingw32-make.exe: * [CL01] Error 2

尝试了好久,终于解决了
方法:
在CMakeLists.txt文件中添加如下代码:

link_libraries(ws2_32)
//*添加位置要注意*

版权声明:本文为lemisky原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/lemisky/article/details/79330118