在某些情况下CMake找不到Boost的可能原因? [英] Possible causes for Boost not being found by CMake in certain situations?
问题描述
我使用 CMake (3.4.1)根据 Boost 库构建C ++项目.主机平台是 Linux ,目标是主机和 cross-build Android NDK.
I build a C++ project depending on the Boost library using CMake (3.4.1). Host platform is Linux, targets are that host and cross-build Android NDK.
我只使用Boost头文件,我只是下载/提取了boost文件夹(而且我没有/usr/include/boost
目录).
I'm only using Boost header files and I just downloaded/extracted the boost folder (and I don't have a /usr/include/boost
directory).
在我的CMakeLists.txt
文件中,我这样声明对Boost的依赖关系:
In my CMakeLists.txt
file I declare the dependency to Boost like this:
find_package(Boost 1.57 REQUIRED)
我将构建配置如下:
BOOST_ROOT=/path/to/boost cmake ../src
实际可以达到我的 native 构建预期的水平.
Which actually works as expected for my native build.
当我现在以完全相同的方式配置构建时(仅指定更多环境变量和CMAKE_TOOLCHAIN_FILE),CMake给了我:
When I now configure a build exactly the same way (only specifying some more environment variables and a CMAKE_TOOLCHAIN_FILE) CMake gives me:
BOOST_ROOT=/path/to/boost JAVA_HOME=/bla/bla/bla \
ANDROID_NDK=/bla/bla/bla \
ANDROID_SDK=/bla/bla/bla \
ANT=/usr/bin/ant \
cmake ../src -DCMAKE_TOOLCHAIN_FILE=/bla/bla/android.toolchain.cmake
CMake Error at /usr/share/cmake/Modules/FindBoost.cmake:1247 (message):
Unable to find the requested Boost libraries.
Unable to find the Boost header files. Please set BOOST_ROOT to the root
directory containing Boost or BOOST_INCLUDEDIR to the directory containing
Boost's headers.
Call Stack (most recent call first):
CMakeLists.txt:4 (find_package)
因此,我相信我为Android目标进行了几乎相同的构建,但在此处无法找到用于主机构建的Boost的完全相同的方法.
So I believe I did almost the same to build for the Android target but the very same method that finds Boost for the host-build doesn't work here.
我尝试将Boost_DIR
,BOOSTROOT
和BOOST_INCLUDEDIR
设置为相同的效果.另外,在尝试任何新操作之前,我已经删除了构建目录中的所有 内容.
I tried to set Boost_DIR
, BOOSTROOT
and BOOST_INCLUDEDIR
all with the same effect. Also I've deleted all content in the build directory before trying anything new.
此行为的可能原因是什么?我已经尝试像这样直接在FindBoost.cmake
脚本中打印BOOST_ROOT
:
What can be possible reasons for this behavior? I've already tried to print BOOST_ROOT
directly in the FindBoost.cmake
script like this:
message("BOOST_ROOT: $ENV{BOOST_ROOT}")
具有预期的行为(编写BOOST_ROOT: /path/to/boost
).
With the expected behavior (writing BOOST_ROOT: /path/to/boost
).
当然我现在可以作弊,只需将boost
文件夹链接到交叉编译器的include
文件夹中,但这当然不好,我想知道发生了什么.
Of course I can cheat now and just link the boost
folder into the include
folder of the cross compiler but that's not nice of course and I want to find out what's going on.
推荐答案
交叉编译时,工具链文件通常会设置变量 CMAKE_FIND_ROOT_PATH_MODE_LIBRARY 变量设置为ONLY
,CMAKE_FIND_ROOT_PATH
变量用作find_library
调用的有效 chroot ,因此仅搜索给定前缀下的库.
When cross-compile, toolchain file normally sets variable CMAKE_FIND_ROOT_PATH. Combined with CMAKE_FIND_ROOT_PATH_MODE_LIBRARY variable set to ONLY
, CMAKE_FIND_ROOT_PATH
variable is used as effective chroot for find_library
call, so only libraries under given prefix(es) are searched.
存在模拟变量,用于调整find_path
(用于搜索包含路径)和find_program
的行为.
Analogue variables exist for adjust behavior for find_path
(used for search include paths) and find_program
.
您使用的工具链文件实际上将CMAKE_FIND_ROOT_PATH
设置为第1521行:
Toolchain file you use actually sets CMAKE_FIND_ROOT_PATH
at line 1521:
set( CMAKE_FIND_ROOT_PATH "${ANDROID_TOOLCHAIN_ROOT}/bin"
"${ANDROID_TOOLCHAIN_ROOT}/${ANDROID_TOOLCHAIN_MACHINE_NAME}"
"${ANDROID_SYSROOT}"
"${CMAKE_INSTALL_PREFIX}"
"${CMAKE_INSTALL_PREFIX}/share" )
及以下将CMAKE_FIND_ROOT_PATH_MODE_*
变量设置为ONLY
.因此,您需要在这些目录之一下安装Boost,并提供相对于它的提示(例如BOOST_ROOT).
and below sets CMAKE_FIND_ROOT_PATH_MODE_*
variables to ONLY
. So you need to have Boost installed under one of these directory, and give hints(like BOOST_ROOT) relative to it.
请注意,Boost应该为目标平台(在您的情况下为Android NDK)而不是在交叉编译的平台(Linux)上构建.
Note, that Boost should be built for target platform (Android NDK in you case), not for the platform where you cross-compile (Linux).
这篇关于在某些情况下CMake找不到Boost的可能原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!