【问题标题】:MSVC build Boost but Cmake findBoost doesn't work?MSVC 构建 Boost 但 Cmake findBoost 不起作用?
【发布时间】:2017-07-12 01:19:04
【问题描述】:

重现我的问题...我download Boost,然后我运行booststrapb2 --build-dir=C:\Users\xxx\Downloads\my_boost_build_dir --prefix=C:\Users\xxx\Downloads\my_boost_install_dir --layout=system variant=release link=static install。到目前为止,一切似乎都很好。提供的前缀(安装)目录填充有headerslibs

但这就是事情开始出错的地方。如果我写下面的cmake文件...

find_package(Boost REQUIRED)

message("Boost_FOUND" ${Boost_FOUND})
message("Boost_INCLUDE_DIRS" ${Boost_INCLUDE_DIRS})
message("Boost_LIBRARY_DIRS" ${Boost_LIBRARY_DIRS})
message("Boost_LIBRARIES" ${Boost_LIBRARIES})
message("Boost_CHRONO_FOUND" ${Boost_CHRONO_FOUND})
message("Boost_CHRONO_LIBRARY" ${Boost_CHRONO_LIBRARY})

add_executable(main main.cpp)
target_link_libraries(main PRIVATE Boost::boost Boost::chrono)

...我用 boost install dir cmake .. -DCMAKE_PREFIX_PATH=C:\Users\xxx\Downloads\my_boost_install_dir 的路径配置它,然后我得到以下输出和错误...

Boost_FOUND1
Boost_INCLUDE_DIRSC:/Users/xxx/Downloads/my_boost_install_dir/include
Boost_LIBRARY_DIRSC:/Users/xxx/Downloads/my_boost_install_dir/lib
Boost_LIBRARIES
Boost_CHRONO_FOUND
Boost_CHRONO_LIBRARY
-- Configuring done
CMake Error at CMakeLists.txt:14 (add_executable):
  Target "main" links to target "Boost::chrono" but the target was not found.
  Perhaps a find_package() call is missing for an IMPORTED target, or an
  ALIAS target is missing?

找到了 Boost,找到了 include 和 lib 目录,但没有找到 chrono 库(和所有其他库)。也许我需要明确命名我的组件?所以我尝试了这个 cmake...

find_package(Boost REQUIRED COMPONENTS chrono)

message("Boost_FOUND" ${Boost_FOUND})
message("Boost_INCLUDE_DIRS" ${Boost_INCLUDE_DIRS})
message("Boost_LIBRARY_DIRS" ${Boost_LIBRARY_DIRS})
message("Boost_LIBRARIES" ${Boost_LIBRARIES})
message("Boost_CHRONO_FOUND" ${Boost_CHRONO_FOUND})
message("Boost_CHRONO_LIBRARY" ${Boost_CHRONO_LIBRARY})

add_executable(main main.cpp)
target_link_libraries(main PRIVATE Boost::boost Boost::chrono)

但这会产生以下输出和错误。

CMake Error at C:/Program Files/CMake/share/cmake-3.9/Modules/FindBoost.cmake:1877 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.64.0

  Boost include path: C:/Users/xxx/Downloads/my_boost_install_dir/include

  Could not find the following Boost libraries:

          boost_chrono

  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:5 (find_package)

Boost_FOUND0
Boost_INCLUDE_DIRSC:/Users/xxx/Downloads/my_boost_install_dir/include
Boost_LIBRARY_DIRS
Boost_LIBRARIES
Boost_CHRONO_FOUND
Boost_CHRONO_LIBRARY

和以前一样,它找到了 boost 和标头,但由于某种原因它找不到库。

【问题讨论】:

  • 如果您需要 Boost 的 chrono 组件,请在 find_package 调用中指定它:find_package(Boost REQUIRED chrono)。请清理问题:由于之前的问题已经解决,只需删除与其对应的文本(除非需要它来描述当前问题)。 Stack Overflow 为所有想查看帖子以前内容的人维护编辑历史记录。
  • @Tsyvarev 我清理了问题并尝试列出组件,但这并没有解决问题。 :-/
  • 嗯,因为你有.lib文件,你可能需要在find_package()之前将Boost_USE_STATIC_LIBS设置为ON。
  • @Tsyvarev 是的!显然这就是秘诀。

标签: boost cmake


【解决方案1】:

如果您想使用特定的(非仅标头)Boost 组件,您必须在find_package 中指定它们。在你的情况下:

find_package(Boost COMPONENTS chrono REQUIRED)

【讨论】:

    【解决方案2】:

    根据https://cmake.org/cmake/help/v3.0/module/FindBoost.html

    find_package(Boost) 按照以下提示查找 boost 目录:

    BOOST_ROOT - 首选安装前缀(或 BOOSTROOT)

    BOOST_INCLUDEDIR - 首选包含目录,例如 /包括

    BOOST_LIBRARYDIR - 首选库目录,例如/lib

    Boost_NO_SYSTEM_PATHS - 设置为 ON 以禁用位置搜索 不是 由这些提示变量指定。默认为关闭。

    Boost_ADDITIONAL_VERSIONS - 此模块未知的 Boost 版本列表 (Boost 安装位置可能包含版本)

    如果您不知道放置 boost 库和包含文件的默认位置在哪里,您应该在 find_package 之前设置这些变量。

    【讨论】:

      猜你喜欢
      • 2019-05-25
      • 2018-11-26
      • 1970-01-01
      • 2010-09-22
      • 2013-12-30
      • 2019-04-21
      • 1970-01-01
      • 2018-07-12
      • 2021-07-05
      相关资源
      最近更新 更多