首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用cmake和Visual Studio 2017找到Boost库

如何用cmake和Visual Studio 2017找到Boost库
EN

Stack Overflow用户
提问于 2017-11-28 13:41:17
回答 1查看 1.3K关注 0票数 4

我用Visual 2017编译了Boost 1.65.1。

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

代码语言:javascript
复制
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)

图书馆在那里:

代码语言:javascript
复制
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:

代码语言:javascript
复制
#   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’标签,但我的名字是那样的。我还试图禁用所有可以使用这些标记的选项,但仍然失败:

代码语言:javascript
复制
 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)
EN

回答 1

Stack Overflow用户

发布于 2018-09-13 09:25:51

关于库命名的

Boost库(.lib,.dll on Win,.a,.so on Linux)名称有3种形式。它们依赖于--layout选项,该选项在构建boost时传递给b2。在Windows上,默认的是versioned,它生成您所拥有的文件名。请看我的详细解释。

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

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

注意:

通常,您不需要设置Boost_USE_MULTITHREADED等变量,因为这些变量是由CMake检测的。从您的"lib“目录文件列表中,我可以看到调试和发布,动态地和静态地链接C运行时版本的库文件。

请参阅这里关于库文件名中的这些字母。

我希望这些都能帮上忙。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47533040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档