本文介绍了如何链接ws2_32在Clion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Clion,它使用MinGW和Cmake。当我尝试使用独立的 asio 库时,我得到

I am using Clion, which uses MinGW and Cmake. When I try to use the standalone asio library I am getting

undefined reference to `WSAStartup@8'
undefined reference to `WSASetLastError@4'
undefined reference to `closesocket@4'
...



我相信我必须链接 C:/Windows/System32/ws2_32.dll 库。我尝试添加 -LC:/ Windows / System32 -lws2_32

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS} -static -lws2_32")

没有帮助。如何解决这些错误?

But that didn't help. How can I fix these errors ?

推荐答案

以下CMakeLists.txt编译时无错误。只需要一行: link_libraries(ws2_32 wsock32)

The following CMakeLists.txt compiled error-less. Only 1 line is really required: link_libraries(ws2_32 wsock32)

cmake_minimum_required(VERSION 3.3)
project(server_client)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -s -O3 -I C:/Users/Shiro/Desktop/asio-1.10.6/include")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS} -static")
link_libraries(ws2_32 wsock32)


set(SOURCE_FILES chat_server.cpp)
add_executable(server_client ${SOURCE_FILES})

这篇关于如何链接ws2_32在Clion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 06:17