Possible causes for Boost not being found by CMake in certain situations?

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.

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).

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.

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)

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.

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.

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}")

With the expected behavior (writing BOOST_ROOT: /path/to/boost).

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.


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.

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

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" )

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.

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