Szymon Marczak Szymon Marczak - 4 months ago 50
C++ Question

How to fix undefined reference to `__imp_WSACleanup' (Boost.Asio) in CLion

My C++ project includes Boost and WebSocket++ libs. I read other topics I need to add

-lws2_32
to gcc command, but I've CLion and I don't know how to fix it.

Console:

CMakeFiles\WsServer.dir/objects.a(main.cpp.obj): In function `boost::asio::detail::winsock_init_base::startup(boost::asio::detail::winsock_init_base::data&, unsigned char, unsigned char)':
C:/MinGW/include/boost/asio/detail/impl/winsock_init.ipp:39: undefined reference to `__imp_WSAStartup'
CMakeFiles\WsServer.dir/objects.a(main.cpp.obj): In function `boost::asio::detail::winsock_init_base::cleanup(boost::asio::detail::winsock_init_base::data&)':
C:/MinGW/include/boost/asio/detail/impl/winsock_init.ipp:56: undefined reference to `__imp_WSACleanup'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\WsServer.dir\build.make:99: WsServer.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:67: CMakeFiles/WsServer.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:79: CMakeFiles/WsServer.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: WsServer] Error 2


What I should do?

Answer

You can pass ws2_32 via target_link_libraries in your CMakeLists.txt, e.g.:

if(MINGW)
  target_link_libraries(YourTargetName ws2_32)
endif()
Comments