How to link C++ program with Boost using CMake


The following is my configuration:

cmake_minimum_required(VERSION 2.8)
set(Boost_INCLUDE_DIR /usr/local/src/boost_1_46_1)
set(Boost_LIBRARY_DIR /usr/local/src/boost_1_46_1/stage/lib)
find_package(Boost COMPONENTS system filesystem REQUIRED)

add_executable(main main.cpp)
target_link_libraries( main ${Boost_LIBRARIES} )

What should my cmake file should look like for linking my program with boost library under Ubuntu?

The errors show during running make:

main.cpp:(.text+0x3b): undefined reference to `boost::program_options::options_description::m_default_line_length'

The main file is really simple:

#include <boost/program_options/options_description.hpp>
#include <boost/program_options/option.hpp>
using namespace std;
#include <iostream>

namespace po = boost::program_options;

int main(int argc, char** argv) {

    po::options_description desc("Allowed options");
        ("help", "produce help message")

    return 0;

I've managed to do that, the only lines that I've added to my cmake files were:


Linking Boost thread library

Ok, I see you've found a solution, but there are some improvements I'd like to propose (as soon as you require CMake 3.6):

  • use imported targets to manage compiler/linker options per target, instead of "global" variables and functions (like include_directories(), ...)
  • use full signature of project() to define a bunch of PROJECT_xxx variables, then use them
  • provide explicit list of languages do you use to avoid default and possible redundant checks
  • in case of Boost, you don't need to find and link with implicit dependencies -- FindBoost.cmake would do it for you. Just specify components only what you really need and use.

Here is a modified CMakeLists.txt:

cmake_minimum_required(VERSION 3.6)
project(LearningCxx VERSION 1.0.0 LANGUAGES CXX)


find_package(Boost COMPONENTS thread REQUIRED)



Update: I've done an example for this project, but remove Students.cpp and replace main.cpp with the following code:

#include <boost/thread.hpp>
#include <iostream>

int main()
    std::cout << boost::thread::hardware_concurrency() << std::endl;

Here is my test run:

zaufi@gentop⟩…/tests/boost-thread-cmake/buildempty dircmake ..
-- The CXX compiler identification is GNU 5.4.0
-- Check for working CXX compiler: /usr/lib/outproc/bin/c++ -- works
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features - done
-- Looking for C++ include pthread.h - found
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Boost version: 1.62.0
-- Found the following Boost libraries:
--   thread
--   chrono
--   system
--   date_time
--   atomic
-- Configuring done
-- Generating done
-- Build files have been written to: /work/tests/boost-thread-cmake/build
zaufi@gentop⟩…/tests/boost-thread-cmake/builddefaultpfx: /usr/localmake
Scanning dependencies of target LearningCxx
[ 50%] Building CXX object CMakeFiles/LearningCxx.dir/main.cpp.o
[100%] Linking CXX executable LearningCxx
[100%] Built target LearningCxx
zaufi@gentop⟩…/tests/boost-thread-cmake/builddefaultpfx: /usr/localldd ./LearningCxx (0x00007fff73b75000) => /usr/lib64/ (0x00007fd1adafd000) => /usr/lib64/ (0x00007fd1ad8f6000) => /usr/lib64/ (0x00007fd1ad6f2000) => /usr/lib64/ (0x00007fd1ad4e1000) => /usr/lib64/ (0x00007fd1ad2df000) => /lib64/ (0x00007fd1ad0c3000) => /usr/lib/gcc/x86_64-pc-linux-gnu/6.3.0/ (0x00007fd1accc8000) => /lib64/ (0x00007fd1ac9cd000) => /usr/lib/gcc/x86_64-pc-linux-gnu/6.3.0/ (0x00007fd1ac7b6000) => /lib64/ (0x00007fd1ac41d000) => /lib64/ (0x00007fd1ac215000)
        /lib64/ (0x00007fd1add25000)
zaufi@gentop⟩…/tests/boost-thread-cmake/builddefaultpfx: /usr/localcmake --version
cmake version 3.7.1

How to link against boost.system with cmake

On linux CMake figures itself that boost_filesystem is linked against boost_system. Obviously you have to tell it explicitly on Mac:

find_package(Boost COMPONENTS system filesystem REQUIRED)

How to include external library (boost) into CLion C++ project with CMake?

After spending the whole afternoon on the issue, I solved it myself. It was a rather stupid mistake and all the hints in @Waxo's answer were really helpful.

The reason why it wasn't working for me that I wrote #include <boost> within my test.cpp-file, which apparently is just wrong. Instead, you need to refer directly to the header files that you actually want to include, so you should rather write e.g. #include <boost/thread.hpp>.

After all, a short sequence of statements should be enough to successfully (and platform-independently) include boost into a CMake project:

find_package(Boost 1.57.0 COMPONENTS system filesystem REQUIRED)
add_executable(BoostTest main.cpp)
target_link_libraries(BoostTest ${Boost_LIBRARIES})

These lines are doing the magic here. For reference, here is a complete CMakeLists.txt file that I used for debugging in a separate command line project:

cmake_minimum_required(VERSION 2.8.4)


message(STATUS "start running cmake...")

find_package(Boost 1.57.0 COMPONENTS system filesystem REQUIRED)


    message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
    message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
    message(STATUS "Boost_VERSION: ${Boost_VERSION}")



add_executable(BoostTest main.cpp)


    target_link_libraries(BoostTest ${Boost_LIBRARIES})