【问题标题】:Error while importing Boost 1.61.0 to C++ project将 Boost 1.61.0 导入 C++ 项目时出错
【发布时间】:2016-08-04 19:07:41
【问题描述】:

我尝试导入 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 里面有什么:

在其他主题中,我看到了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。

【问题讨论】:

  • 您是否使用您在这里使用的任何编译器编译了 boost?听起来你刚刚在C:/Users/Marczak/boost_1_61_0 提取了源代码
  • @drescherjm 不,我是 C++ 新手。怎么编译?
  • 也许 mingw 的 boost 二进制下载对你来说会更好。话虽如此,我不确定 CLion 在 Windows 下使用的是什么编译器/工具包。是mingw/gcc吗??
  • @drescherjm 是的。
  • @SzymonMarczak 您可能想要添加 clion 和 mingw 标签,因为它们非常集中地描述了您的问题,不是吗?

标签: c++ boost clion


【解决方案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_ROOTBOOST_INCLUDEDIR + BOOST_LIBRARYDIR,如果需要,您应该设置它们。您不必使用我链接的 MinGW 发行版,因为它已经在可访问的位置包含 boost 包含和库。
  • 您可以通过查看 CMake 缓存中的 Boost_* 变量来检查库的路径是否正确。
  • libs boost 源中的目录与问题无关,它不包含任何二进制文件
  • 您已下载使用 Visual Studio 工具链而非 MinGW 构建的 boost 二进制文件,因此它们与您的设置不兼容。如果您不想使用我链接的 MinGW 包,则必须找到使用正确 MinGW 版本构建的 boost 二进制文件或自己构建。

【讨论】:

  • 成功了!非常感谢。
猜你喜欢
  • 2018-07-22
  • 2014-10-26
  • 2015-05-13
  • 2018-08-24
  • 2019-09-24
  • 2014-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多