我正在接近这组库,因为我需要为我的大学考试创建一个个人项目,并且我想做一些具有网络功能的事情。我已经有一本关于这个特定库的书,但现在我无法在我的项目中链接它(我的书只解释了如何在 linux 上将它与 GCC 链接,但我正在 Windows 10 上将 CLion 与 MinGW 结合起来)。当我像这样编译一个简单的空项目时:
#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>
升压变红,静态分析器告诉我“找不到升压”。现在问题出在哪里?谢谢!