在某些情况下,CMake 找不到 Boost 的可能原因? [英] Possible causes for Boost not being found by CMake in certain situations?

查看:23
本文介绍了在某些情况下,CMake 找不到 Boost 的可能原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 CMake (3.4.1) 根据 Boost 库构建了一个 C++ 项目.主机平台是Linux,目标是主机和交叉构建 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)

我像这样配置我的构建:

And I configure my build like this:

BOOST_ROOT=/path/to/boost cmake ../src

对于我的本机构建,它​​实际上工作.

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_DIRBOOSTROOTBOOST_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.结合 CMAKE_FIND_ROOT_PATH_MODE_LIBRARY 变量设置为 ONLYCMAKE_FIND_ROOT_PATH 变量用作 find_library 调用的有效 chroot,因此只有给定前缀下的库是搜索.

When cross-compiling, the toolchain file normally sets the variable CMAKE_FIND_ROOT_PATH. Combined with the CMAKE_FIND_ROOT_PATH_MODE_LIBRARY variable set to ONLY, CMAKE_FIND_ROOT_PATH variable is used as effective chroot for find_library calls, so only libraries under the given prefix(es) are searched.

模拟变量的存在是为了调整 find_path(用于搜索包含路径)和 find_program 的行为.

Analogue variables exist to adjust the behavior for find_path (used for searching include paths) and find_program.

您使用的工具链文件实际上在 第 1521 行:

THe 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 directories, and give hints (like BOOST_ROOT) relative to it.

请注意,Boost 应该是为目标平台(在您的情况下为 Android NDK)构建的,而不是为您交叉编译的平台(Linux)构建的.

Note, that Boost should be built for the target platform (Android NDK in you case), not for the platform where you cross-compile (Linux).

这篇关于在某些情况下,CMake 找不到 Boost 的可能原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆