要重现我的问题...我download Boost,然后我运行booststrap
和b2 --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
。一切看起来都那么好。提供的前缀(安装)目录中填充了headers和libs。
但是这里的事情开始出错了。如果我写下面的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 dirs,但是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和标题,但由于某些原因它无法找到库。
答案 0 :(得分:1)
If you want to use specific (non-header-only) Boost components, you have to specify them in find_package
. In your case:
find_package(Boost COMPONENTS chrono REQUIRED)
答案 1 :(得分:0)
根据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以禁用在位置搜索 不 由这些提示变量指定。默认为OFF。
Boost_ADDITIONAL_VERSIONS - 此模块未知的Boost版本列表 (Boost安装位置可能包含版本)
如果您不知道放置boost库和包含文件的默认位置在哪里,则应在find_package之前设置这些变量。