skip to Main Content

I need to build a C++ program with CMake but it cannot find Boost.

I’m getting the following error no matter what solutions I’ve found online for other people with this problem…

-- Compiling with C++ standard: 17
CMake Error at /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Boost (missing: Boost_INCLUDE_DIR) (Required is at least
  version "1.68.0")
Call Stack (most recent call first):
  /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.22/Modules/FindBoost.cmake:2360 (find_package_handle_standard_args)
  CMakeLists.txt:49 (find_package)

This is the part of my CMakeLists.txt looking for Boost

if(NUMCPP_NO_USE_BOOST)
    target_compile_definitions(${ALL_INTERFACE_TARGET} INTERFACE -DNUMCPP_NO_USE_BOOST)
else()
    find_package(Boost 1.68.0 REQUIRED)
    target_link_libraries(${ALL_INTERFACE_TARGET} INTERFACE Boost::boost)
endif()

I have installed boost with sudo apt-get install libboost-all-dev and a few other apt-get options. I always see 0 upgraded, 0 newly installed, 0 to remove after running.

$ whereis boost
boost: /usr/include/boost

Do I need to specify it in the cmake .. line in the terminal somehow?

2

Answers


  1. Assuming that you’ve successfully installed boost in the default directory, and CMake has access to those directories, probably the problem (as @drescherjm mentioned) is in the version of the Boost you’ve installed (sometimes the packages provided by apt-get are relatively old).
    On the other hand it can also be that CMake doesn’t have the installation directory in its search path.

    The following steps might be a good place to start:

    1. Check the version of the Boost installed: How to determine the Boost version on a system? and here

    If the version installed by apt-get satisfies the project requirement, it seems for some reason CMake didn’t include a directory of installation to its searching path. If that’s the case, skip to the step 3.

    1. If it’s lower than your project requires, build it from source (right now 1.80 is available) to your preferred directory. If you need the libraries which need to be built, check this tutorial.
    2. Tell CMake where to search for built Boost. See here: Cmake doesn't find Boost
    Login or Signup to reply.
  2. I hope this reply is not too off-topic. I suggest you consider using a package manager such as conan or vcpkg.

    An example of using conan to use external dependencies.

    1. Create conanfile.txt

    It should have the following content:

    [requires]
    boost/1.80
    
    [generators]
    CMakeDeps
    CMakeToolchain
    

    2. Download your dependencies

    mkdir build && cd build
    conan install .. --build=missing
    conan build ..
    

    The last command is optional and can be changed with:

    cmake -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake ..
    

    Benefits:

    • You don’t need to "mess" your system with different libraries
    • Transitional dependencies are handled automatically
    • The build process is reproducible
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search