【问题标题】:How to find Boost libraries with cmake and Visual Studio 2017如何使用 cmake 和 Visual Studio 2017 查找 Boost 库
【发布时间】:2017-11-28 13:41:17
【问题描述】:

我使用 Visual Studio 2017 编译了 Boost 1.65.1。

使用 cmake 编译某些 C++ 项目时,找不到 Boost 库:

cmake -G"Visual Studio 15 2017" -DBoost_COMPILER="-vc150" ..

CMake Error at cmake-3.9/Modules/FindBoost.cmake:1900 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.65.1

  Boost include path: C:/Boost/include/boost-1_65_1

  Could not find the following Boost libraries:

          boost_thread
          boost_system
          boost_regex
          boost_timer
          boost_filesystem
          boost_serialization

  Some (but not all) of the required Boost libraries were found.  You may
  need to install these additional Boost libraries.  Alternatively, set
  BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT
  to the location of Boost.
Call Stack (most recent call first):
  CMakeLists.txt:18 (find_package)

图书馆在那里:

dir c:\Boost\lib
 Volume in drive C has no label.

 Directory of c:\Boost\lib

28/11/2017  12:47    <DIR>          .
28/11/2017  12:47    <DIR>          ..
...
28/11/2017  12:12           937.796 libboost_filesystem-vc150-mt-1_65_1.lib
28/11/2017  12:40         6.792.370 libboost_filesystem-vc150-mt-gd-1_65_1.lib
28/11/2017  12:20           996.592 libboost_filesystem-vc150-mt-s-1_65_1.lib
28/11/2017  12:17         5.951.092 libboost_filesystem-vc150-mt-sgd-1_65_1.lib
28/11/2017  12:28           996.456 libboost_filesystem-vc150-s-1_65_1.lib
28/11/2017  12:25         5.950.956 libboost_filesystem-vc150-sgd-1_65_1.lib
...
28/11/2017  12:13        13.418.950 libboost_regex-vc150-mt-1_65_1.lib
28/11/2017  12:40        43.663.182 libboost_regex-vc150-mt-gd-1_65_1.lib
28/11/2017  12:21        13.487.852 libboost_regex-vc150-mt-s-1_65_1.lib
28/11/2017  12:17        42.108.148 libboost_regex-vc150-mt-sgd-1_65_1.lib
28/11/2017  12:28        13.487.546 libboost_regex-vc150-s-1_65_1.lib
28/11/2017  12:25        42.107.842 libboost_regex-vc150-sgd-1_65_1.lib
28/11/2017  12:16         9.426.284 libboost_serialization-vc150-mt-1_65_1.lib
28/11/2017  12:43        33.389.696 libboost_serialization-vc150-mt-gd-1_65_1.lib
28/11/2017  12:24        10.690.636 libboost_serialization-vc150-mt-s-1_65_1.lib
28/11/2017  12:20        34.091.376 libboost_serialization-vc150-mt-sgd-1_65_1.lib
28/11/2017  12:31        10.690.106 libboost_serialization-vc150-s-1_65_1.lib
28/11/2017  12:27        34.090.846 libboost_serialization-vc150-sgd-1_65_1.lib
...
             200 File(s)  2.184.720.662 bytes
               2 Dir(s)  48.071.262.208 bytes free

如何在此配置中使用 Boost?

编辑:

我发现存在命名问题。来自 FindBoost.cmake:

#   Boost_USE_MULTITHREADED  - Set to OFF to use the non-multithreaded
#                              libraries ('mt' tag).  Default is ON.
#   Boost_USE_STATIC_LIBS    - Set to ON to force the use of the static
#                              libraries.  Default is OFF.
#   Boost_USE_STATIC_RUNTIME - Set to ON or OFF to specify whether to use
#                              libraries linked statically to the C++ runtime
#                              ('s' tag).  Default is platform dependent.
#   Boost_USE_DEBUG_RUNTIME  - Set to ON or OFF to specify whether to use
#                              libraries linked to the MS debug C++ runtime
#                              ('g' tag).  Default is ON.
#   Boost_USE_DEBUG_PYTHON   - Set to ON to use libraries compiled with a
#                              debug Python build ('y' tag). Default is OFF.
#   Boost_USE_STLPORT        - Set to ON to use libraries compiled with
#                              STLPort ('p' tag).  Default is OFF.
#   Boost_USE_STLPORT_DEPRECATED_NATIVE_IOSTREAMS
#                            - Set to ON to use libraries compiled with
#                              STLPort deprecated "native iostreams"
#                              ('n' tag).  Default is OFF.
#   Boost_COMPILER           - Set to the compiler-specific library suffix
#                              (e.g. "-gcc43").  Default is auto-computed
#                              for the C++ compiler in use.  A list may be
#                              used if multiple compatible suffixes should
#                              be tested for, in decreasing order of
#                              preference.
#   Boost_THREADAPI          - Suffix for "thread" component library name,
#                              such as "pthread" or "win32".  Names with
#                              and without this suffix will both be tried.
#   Boost_NAMESPACE          - Alternate namespace used to build boost with
#                              e.g. if set to "myboost", will search for
#                              myboost_thread instead of boost_thread.

没有提到“gd”或“sgd”标签,但我的库是这样命名的。 我还尝试禁用所有可以使用这些标签的选项,但仍然失败:

 cmake -G"Visual Studio 15 2017" -DBoost_COMPILER="-vc150" -DBOOST_LIBRARYDIR=/c/Boost/lib -DBoost_USE_STATIC_RUNTIME=OFF -DBoost_USE_DEBUG_RUNTIME=OFF ..

CMake Error at cmake-3.9/Modules/FindBoost.cmake:1900 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.65.1

  Boost include path: C:/Boost/include/boost-1_65_1

  Could not find the following Boost libraries:

          boost_thread
          boost_system
          boost_regex
          boost_timer
          boost_filesystem
          boost_serialization

  Some (but not all) of the required Boost libraries were found.  You may
  need to install these additional Boost libraries.  Alternatively, set
  BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT
  to the location of Boost.
Call Stack (most recent call first):
  CMakeLists.txt:18 (find_package)

【问题讨论】:

  • 捆绑的 cmake 可以在这里从 boost 中找到预构建的 windows 二进制文件:dl.bintray.com/boostorg/release/1.65.1/binaries。主要区别在于库是按架构和编译器版本组织的,因此路径如下:C:\local\boost_1_65_1\lib32-msvc-14.1 和 C:\local\boost_1_65_1\lib64-msvc-14.1。也许它只是在那些文件夹中寻找库?

标签: boost cmake visual-studio-2017


【解决方案1】:

关于库命名:

Boost 库(Win 上为 .lib、.dll,Linux 上为 .a、.so)名称有 3 种形式。它们依赖于在构建 boost 时传递给 b2--layout 选项。在 Windows 上,默认值为 versioned,它会生成您拥有的文件名。 See my detailed explanation here.

您的问题的可能解决方案:

  1. 在第二次尝试中,您致电
    cmake -G"Visual Studio 15 2017" -DBoost_COMPILER="-vc150" -DBOOST_LIBRARYDIR=/c/Boost/lib ...
    但是我不确定路径 /c/Boost/lib 是否适用于 Windows CMake,请尝试 C:\Boost\lib
    顺便说一句,我会简单地定义 BOOST_ROOT=C:\Boost 而不是 BOOST_LIBRARYDIR
  2. 由于您只有库而不是 DLL,因此请确保您的 CMake 文件中的 Boost_USE_STATIC_LIBSON(或在命令行中传递)。 如果您碰巧也使用 python 和/或 numpy 库,那么当您包含来自这些库的头文件时(例如:add_definitions(-DBOOST_PYTHON_STATIC_LIB -DBOOST_NUMPY_STATIC_LIB)),您还需要为编译器定义BOOST_PYTHON_STATIC_LIBBOOST_NUMPY_STATIC_LIB
  3. 开启升压诊断: -DBoost_DEBUG=ON
    CMake 文件打印了很多关于它如何尝试处理 boost 的信息。

注意:
通常您不需要设置Boost_USE_MULTITHREADED 等变量,因为这些是由CMake 检测到的。从您的“lib”目录文件列表中,我可以看到库文件的调试和发布、动态和静态链接的 C 运行时版本都存在。
See here about these letters in the library file names.

我希望其中一些有所帮助。

【讨论】:

    猜你喜欢
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多