Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

c++ - How to link ws2_32 in Clion

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'
...

I believe I have to link the C:/Windows/System32/ws2_32.dll library. I tried adding something like -L C:/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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...