将Boost 1.61.0导入C ++项目

时间:2016-08-03 18:01:17

标签: c++ boost clion

我尝试导入Boost 1.61.0(从SourceForge - Boost 1.61.0下载为.7z),但失败了。

控制台:

"D:\Program Files (x86)\JetBrains\CLion 2016.2\bin\cmake\bin\cmake.exe" --build C:\Users\Marczak\.CLion2016.2\system\cmake\generated\WsServer-e351c9f9\e351c9f9\Debug --target WsServer -- -j 4
[ 50%] Linking CXX executable WsServer.exe
CMakeFiles\WsServer.dir\build.make:96: recipe for target 'WsServer.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/WsServer.dir/all' failed
CMakeFiles\WsServer.dir/objects.a(main.cpp.obj): In function `_static_initialization_and_destruction_0':
C:/Users/Marczak/boost_1_61_0/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'
C:/Users/Marczak/boost_1_61_0/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
C:/Users/Marczak/boost_1_61_0/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [WsServer.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/WsServer.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/WsServer.dir/rule] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/WsServer.dir/rule' failed
mingw32-make.exe: *** [WsServer] Error 2
Makefile:117: recipe for target 'WsServer' failed

的CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
project(WsServer)

set(BOOST_ROOT "C:/Users/Marczak/boost_1_61_0")

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

set(SOURCE_FILES src/main.cpp)

find_package(Boost)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(WsServer ${SOURCE_FILES})

如果我find_package(Boost 1.61.0 COMPONENTS system filesystem REQUIRED)我得到:

Error: Unable to find the requested Boost libraries.
Boost version: 1.61.0
Boost include path: C:/Users/Marczak/boost_1_61_0
Could not find the following static Boost libraries:
        boost_system         boost_filesystem
No Boost libraries were found.  You may need to set BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT to the location of Boost.

我尝试设置Boost_USE_STATIC_LIBRARIES,但也失败了。我使用CLion 2016.2。

更新:我也尝试过旧版本。同样的错误。什么在.7z内:

.7z file

在其他主题中,我看到lib文件夹。但在这里我没有看到它。我应该放在BOOST_LIBRARYDIR

更新2:来自https://sourceforge.net/projects/boost/files/boost-binaries/1.61.0/的已安装二进制文件。我注意到有新文件夹:lib64-msvc-14.0。它包含许多.dll和.lib文件,例如boost_atomic-vc140-mt-1_61.dll

Boost.org说:

  

如果您打算在Windows命令提示符下使用您的工具,那么您就在正确的位置。如果您计划从Cygwin bash shell构建,那么您实际上是在POSIX平台上运行,并且应该按照Unix变体入门的说明进行操作。不支持其他命令shell,例如MinGW的MSYS, - 它们可能有效也可能无效

我会尝试使用Cygwin。

1 个答案:

答案 0 :(得分:1)

如果您是C ++新手,我建议您下载由Stephan T. Lavavej(Microsoft C ++开发人员)维护的MinGW发行版:https://nuwen.net/mingw.html。除了其他工具和库之外,它还包含预构建的boost二进制文件。将其解压缩并通过Settings | Build, Execution, Deployment | Toolchains指定其路径。

之后,您应该能够使用以下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)

find_package(Boost REQUIRED COMPONENTS filesystem)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(WsServer ${SOURCE_FILES})
target_link_libraries(WsServer ${Boost_LIBRARIES})

请勿忘记删除CMake缓存,因为find_packages由于性能原因而无法更新成功结果(在CLion中可以通过Cmake toolbar | Cache | red arrows icon完成)。

其他一些评论:

  • Boost_USE_STATIC_LIBRARIES并非意在手动设置,而是通过运行使用find_package(Boost)BOOST_ROOT的{​​{1}}进行设置,您应该根据需要进行设置。您不必使用已关联的MinGW发行版,因为它已在可访问的位置启用了包含和库。
  • 您可以通过查看CMake缓存中的BOOST_INCLUDEDIR + BOOST_LIBRARYDIR变量来检查库的路径是否正确。
  • 增强源中的
  • Boost_*目录与问题无关,它不会包含任何二进制文件
  • 您已经下载了使用Visual Studio工具链而不是MinGW构建的boost二进制文件,因此它们与您的设置不兼容。如果您不想使用我已经链接过的MinGW软件包,您必须找到使用正确的MinGW版本构建的boost二进制文件,或者自己构建它。