如何在Clion中链接ws2_32

时间:2016-04-09 17:28:18

标签: c++ cmake mingw boost-asio clion

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

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

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

之类的内容
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS} -static -lws2_32")

但这并没有帮助。我该如何解决这些错误?

1 个答案:

答案 0 :(得分:2)

以下CMakeLists.txt编译无错误。实际上只需要1行: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})