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

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

I have included Boost 1.61.0. My C++ project had similar problem, I added

target_link_libraries(WsServer ws2_32)
and fixed it. Then, another error appeared. The code: WebSocket++ examples: simple_broadcast_server.cpp . How I can fix it?

Console:

CMakeFiles\WsServer.dir/objects.a(main.cpp.obj): In function `boost::asio::detail::socket_ops::complete_iocp_accept(unsigned long long, void*, unsigned long, sockaddr*, unsigned long long*, unsigned long long, boost::system::error_code&)':
C:/MinGW/include/boost/asio/detail/impl/socket_ops.ipp:199: undefined reference to `GetAcceptExSockaddrs'
CMakeFiles\WsServer.dir/objects.a(main.cpp.obj): In function `boost::asio::detail::win_iocp_socket_service_base::start_accept_op(boost::asio::detail::win_iocp_socket_service_base::base_implementation_type&, bool, boost::asio::detail::socket_holder&, int, int, int, void*, unsigned long, boost::asio::detail::win_iocp_operation*)':
C:/MinGW/include/boost/asio/detail/impl/win_iocp_socket_service_base.ipp:495: undefined reference to `AcceptEx'
CMakeFiles\WsServer.dir/objects.a(main.cpp.obj): In function `boost::asio::detail::win_iocp_socket_service_base::restart_accept_op(unsigned long long, boost::asio::detail::socket_holder&, int, int, int, void*, unsigned long, boost::asio::detail::win_iocp_operation*)':
C:/MinGW/include/boost/asio/detail/impl/win_iocp_socket_service_base.ipp:520: undefined reference to `AcceptEx'
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


CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
project(WsServer)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

set(SOURCE_FILES src/main.cpp)

include_directories(C:/Users/Marczak/CPPLibs)
find_package(Boost REQUIRED COMPONENTS filesystem)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(WsServer ${SOURCE_FILES})
target_link_libraries(WsServer ${Boost_LIBRARIES})

target_link_libraries(WsServer ws2_32)

Answer

It seems that you have to add wsock32 to link libraries, see this post.