[cmake-developers] FindBoost.cmake cannot find some libraries when cross-compiling

Guillaume Papin guillaume.papin at parrot.com
Wed Oct 15 10:04:49 EDT 2014


Hello CMake developers,

I have a cross-compiled version of Boost and some other libraries under ~/my-project/CrossThirdPartyPrefix.

I invoke CMake with a CMAKE_TOOLCHAIN_FILE and also I sets CMAKE_FIND_ROOT_PATH
to the directory where my cross-compiled Boost is installed.

    cmake -DCMAKE_TOOLCHAIN_FILE=$HOME/my-project/plateforms/arm-toolchain.cmake \
        -DCMAKE_FIND_ROOT_PATH=$HOME/my-project/CrossThirdPartyPrefix \
        ~/my-projects/src/

But I have an error when using FindBoost.cmake:

In my project's CMakeLists.txt:
    find_package(Boost 1.53.0 REQUIRED COMPONENTS atomic thread system)

The error:
     CMake Error at .../share/cmake-3.0/Modules/FindBoost.cmake:1179 (message):
       Unable to find the requested Boost libraries.

       Boost version: 1.55.0

       Boost include path:
       ~/myproject/CrossThirdPartyPrefix/include


       Could not find the following Boost libraries:

               boost_thread
               boost_system

       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.


So the first component library (atomic) is found but not the other ones.

Looking at the code, when the first library is found, Boost_LIBRARY_DIR is set
to the directory that contains the library and then change the further calls of
find_library to use Boost_LIBRARY_DIR as the unique HINTS, and with the
NO_DEFAULT_PATH options set.

    macro(_Boost_FIND_LIBRARY var)
      find_library(${var} ${ARGN})

      if(${var})
        # If this is the first library found then save Boost_LIBRARY_DIR.
        if(NOT Boost_LIBRARY_DIR)
          get_filename_component(_dir "${${var}}" PATH)
          set(Boost_LIBRARY_DIR "${_dir}" CACHE PATH "Boost library directory" FORCE)
        endif()
      elseif(_Boost_FIND_LIBRARY_HINTS_FOR_COMPONENT)
        # Try component-specific hints but do not save Boost_LIBRARY_DIR.
        find_library(${var} HINTS ${_Boost_FIND_LIBRARY_HINTS_FOR_COMPONENT} ${ARGN})
      endif()

      # If Boost_LIBRARY_DIR is known then search only there.
      if(Boost_LIBRARY_DIR)
        set(_boost_LIBRARY_SEARCH_DIRS ${Boost_LIBRARY_DIR} NO_DEFAULT_PATH)
      endif()
    endmacro()

* https://github.com/Kitware/CMake/blob/1b3495d32e1523648da08e138482a654f8765333/Modules/FindBoost.cmake#L322-L325
    
The issue is that, when using CMAKE_FIND_ROOT_PATH, the Boost_LIBRARY_DIR of the
host (~/myproject/CrossThirdPartyPrefix/lib/ in my case), will be expanded against the root paths.

To find my libraries I can apply the following fix but I'm not sure if it's the
right solution or not.

    diff --git a/Modules/FindBoost.cmake b/Modules/FindBoost.cmake
    index 3642b3e..aad6575 100644
    --- a/Modules/FindBoost.cmake
    +++ b/Modules/FindBoost.cmake
    @@ -321,7 +321,7 @@ macro(_Boost_FIND_LIBRARY var)
     
       # If Boost_LIBRARY_DIR is known then search only there.
       if(Boost_LIBRARY_DIR)
    -    set(_boost_LIBRARY_SEARCH_DIRS ${Boost_LIBRARY_DIR} NO_DEFAULT_PATH)
    +    set(_boost_LIBRARY_SEARCH_DIRS ${Boost_LIBRARY_DIR} NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
       endif()
     endmacro()
     
    @@ -855,7 +855,7 @@ if(_Boost_CHANGE_LIBDIR AND NOT _Boost_LIBRARY_DIR_CHANGED)
     endif()
     
     if(Boost_LIBRARY_DIR)
    -  set(_boost_LIBRARY_SEARCH_DIRS ${Boost_LIBRARY_DIR} NO_DEFAULT_PATH)
    +  set(_boost_LIBRARY_SEARCH_DIRS ${Boost_LIBRARY_DIR} NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
     else()
       set(_boost_LIBRARY_SEARCH_DIRS "")
       if(BOOST_LIBRARYDIR)


What I'm wondering is, whether or not Boost_LIBRARY_DIR should be a path on the target or on the host?

Right now, the workaround I have that doesn't require to modify FindBoost.cmake
is to set Boost_LIBRARY_DIR to /lib/, which will be expanded against the CMAKE_FIND_ROOT_PATH.

By the way, I'm a happy CMake user, thanks for all the work!
Guillaume



More information about the cmake-developers mailing list