Cmake doesn't find Boost

Viewed 232613

I'm trying to configure a project using CMake, but it fails to find Boost libraries even though they are in the specified folder. I have specified Boost_INCLUDE_DIR, Boost_LIBRARYDIR and BOOST_ROOT , but I still get an error saying that CMake is not able to find Boost. What could be the reason of such error?

15 Answers

For me this error was simply because boost wasn't installed so on ubuntu:

sudo apt install build-essential libboost-system-dev libboost-thread-dev libboost-program-options-dev libboost-test-dev

In my case Boost was not installed. I used below command on Mac and then cmake find_package(Boost) works like a charm

brew install Boost

Please note upper case 'B' in Boost!

See FindBoost.cmake first. The variables you set are the correct ones but they should be all uppercase.

Make sure the library architecture matches with CMake configuration.

cmake -A x64 ..

I suggest creating a minimal executable which only includes a Boost library to see if it compiles.

#include <iostream>
#include <boost/date_time.hpp>
int main() {
    using namespace std;
    using namespace boost::gregorian;
    date today = day_clock::local_day();
    cout << today << endl;
}
find_package(Boost REQUIRED COMPONENTS
  date_time
)

include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIRS})

add_executable(test_boost "test_boost.cpp")
target_link_libraries(test_boost Boost::date_time)

Start debugging by checking Boost_FOUND first.

message(STATUS "Boost_FOUND: ${Boost_FOUND}")

The version should be found even if no libraries are found. (Boost_VERSION)

If Boost_LIBRARY_DIRS becomes non-empty, it should compile.

I had the same problem, and none of the above solutions worked. Actually, the file include/boost/version.hpp could not be read (by the cmake script launched by jenkins).

I had to manually change the permission of the (boost) library (even though jenkins belongs to the group, but that is another problem linked to jenkins that I could not figure out):

chmod o+wx ${BOOST_ROOT} -R # allow reading/execution on the whole library
#chmod g+wx ${BOOST_ROOT} -R # this did not suffice, strangely, but it is another story I guess

This can also happen if CMAKE_FIND_ROOT_PATH is set as different from BOOST_ROOT. I faced the same issue that in spite of setting BOOST_ROOT, I was getting the error. But for cross compiling for ARM I was using Toolchain-android.cmake in which I had (for some reason):

set(BOOST_ROOT "/home/.../boost")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --sysroot=${SYSROOT}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --sysroot=${SYSROOT} -I${SYSROOT}/include/libcxx")
set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS}")
set(CMAKE_FIND_ROOT_PATH "${SYSROOT}")

CMAKE_FIND_ROOT_PATH seems to be overriding BOOST_ROOT which was causing the issue.

Maybe

brew install boost

will help you.

Related