在CLion中链接Boost.Asio时出现Static_initialization_and_distruction错误

时间:2017-03-31 19:06:57

标签: c++ boost asio

我正在接近这套图书馆因为我需要为我的大学考试创建一个个人项目,我想对网络功能做些什么。我已经有了一本关于这个特定库的书,但现在我无法在我的项目中链接这个(我的书只解释了如何在Linux上与GCC链接它,但我在Windows 10上使用MinGW来克服CLion)。 当我编译一个像这样的简单空项目时:

#include <boost/asio.h>

int main ( int argc, char ** argv ) {

   return 0;
}

我得到了这个令人印象深刻的错误:

   CMakeFiles\Hello.dir/objects.a(main.cpp.obj): In function `_static_initialization_and_destruction_0':
   C:/boost_1_63_0/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'
   C:/boost_1_63_0/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
   C:/boost_1_63_0/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'
   CMakeFiles\Hello.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio5error19get_system_categoryEv':
   C:/boost_1_63_0/boost/asio/error.hpp:230: undefined reference to `boost::system::system_category()'
   CMakeFiles\Hello.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh':
   C:/boost_1_63_0/boost/asio/detail/impl/winsock_init.ipp:39: undefined reference to `WSAStartup@8'
   CMakeFiles\Hello.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE':
   C:/boost_1_63_0/boost/asio/detail/impl/winsock_init.ipp:56: undefined        reference to `WSACleanup@0'
   collect2.exe: error: ld returned 1 exit status
   CMakeFiles\Hello.dir\build.make:96: recipe for target 'Hello.exe' failed
   CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Hello.dir/all' failed
   mingw32-make.exe[3]: *** [Hello.exe] Error 1
   mingw32-make.exe[2]: *** [CMakeFiles/Hello.dir/all] Error 2
   mingw32-make.exe[1]: *** [CMakeFiles/Hello.dir/rule] Error 2
   CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Hello.dir/rule' failed
   mingw32-make.exe: *** [Hello] Error 2
   Makefile:117: recipe for target 'Hello' failed

这是我的CMakeLists.txt

   cmake_minimum_required(VERSION 3.6)
   project(Hello)


   set(CMAKE_CXX_STANDARD 14)

   set(SRC_FILES main.cpp)
   include_directories(C:\\boost_1_63_0)
   link_directories(C:\\boost_1_63_0)

   find_package(BOOST 1.63.0 REQUIRED)

   add_executable(Hello ${SRC_FILES})

我该如何解决这个问题? 提前谢谢!

编辑

我更新了我的CMakeLists.txt,如下所示:

cmake_minimum_required(VERSION 3.8.0)
project(Hello)


set(CMAKE_CXX_STANDARD 14)

set(SRC_FILES main.cpp)

set(BOOST_ROOT C:\\boost_1_63_0)
find_package(Boost 1.63.0 COMPONENTS system REQUIRED)
find_library(WS2_32_LIBRARY ws2_32)

link_directories(C:\\boost_1_63_0)
link_libraries(${BOOST_LIBRARIES})

add_executable(Hello ${SRC_FILES})
target_link_libraries(Hello ${WS2_32_LIBRARY} ${BOOST_LIBRARIES})

现在,当我告诉我的节目时,我没有任何错误:

#include <boost/asio.hpp>

提升变红,静态分析仪告诉我“无法找到提升”。 现在问题在哪里? 谢谢!

1 个答案:

答案 0 :(得分:0)

您应该与Boost.SystemWinsock相关联,因此请在CMakeLists.txt中添加以下内容:

find_package(Boost COMPONENTS system REQUIRED)
find_library(WS2_32_LIBRARY ws2_32)
target_link_libraries(Hello ${WS2_32_LIBRARY} ${Boost_SYSTEM_LIBRARY})