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

77,623

Solution 1

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)
include_directories(${Boost_INCLUDE_DIRS})
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)

project(BoostTest)

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

find_package(Boost 1.57.0 COMPONENTS system filesystem REQUIRED)

if(Boost_FOUND)

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

    include_directories(${Boost_INCLUDE_DIRS})

endif()

add_executable(BoostTest main.cpp)

if(Boost_FOUND)

    target_link_libraries(BoostTest ${Boost_LIBRARIES})

endif()

Solution 2

Try using CMake find_package(Boost)

src : http://www.cmake.org/cmake/help/v3.0/module/FindBoost.html

It works better and CMake is made for cross compilation and giving an absolute path is not good in a CMake project.

Edit:

Look at this one too : How to link C++ program with Boost using CMake

Because you don't link actually the boost library to your executable.

CMake with boost usually looks like that :

set(Boost_USE_STATIC_LIBS        ON) # only find static libs
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME    OFF)
find_package(Boost 1.57.0 COMPONENTS date_time filesystem system ...)
if(Boost_FOUND)
  include_directories(${Boost_INCLUDE_DIRS})
  add_executable(foo foo.cc)
  target_link_libraries(foo ${Boost_LIBRARIES})
endif()
Share:
77,623
nburk
Author by

nburk

learning &amp; teaching @ prisma.io https://twitter.com/nikolasburk/status/1093913218676940805

Updated on July 09, 2022

Comments

  • nburk
    nburk almost 2 years

    I have the following setup for C++ development:

    • OS X Yosemite
    • CLion 140.2310.6 (a cross-plattform C/C++-IDE by JetBrains using CMake as build system)
    • installed boost via brew install boost into /usr/local/Cellar/boost/

    Now, my goal is to setup a simple project and include the boost library. I defined just one test.cpp file that looks like this:

    #include <iostream>
    #include <boost>
    
    using namespace std;
    
    int test() {
        cout << "Hello, World!" << endl;
        return 0; 
    }
    

    My CMakeLists.txt file looks like this:

    cmake_minimum_required(VERSION 2.8.4) 
    project(MyProject)
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 
    
    include_directories("/usr/local/Cellar/boost/1.57.0/include/boost")
    
    set(SOURCE_FILES main.cpp ./test.cpp)
    add_executable(MyProject ${SOURCE_FILES}) 
    

    When I build the project, I get the following error:

    /Users/nburk/Documents/uni/master/master_thesis/MyProject/test.cpp:2:10: fatal error: 'boost' file not found

    make[3]: *** [CMakeFiles/MyProject.dir/test.cpp.o] Error 1 make[2]: *** [CMakeFiles/MyProject.dir/all] Error 2 make[1]: *** [CMakeFiles/MyProject.dir/rule] Error 2 make: *** [MyProject] Error 2

    I played around with adjusting paths here and there and also using add_library and target_link_libraries, none of which made the project build successfully.

    Can someone point into the right direction how to make sure I can include boosts functionality into my CLion C++ project?

    Update: Thanks to @Waxo's answer I used the following code in my CMakeLists.txt file which:

    set(Boost_INCLUDE_DIR /usr/local/Cellar/boost/1.57.0)
    set(Boost_LIBRARY_DIR /usr/local/Cellar/boost/1.57.0/lib)
    find_package(Boost COMPONENTS system filesystem REQUIRED)
    include_directories(${Boost_INCLUDE_DIR})
    

    I now got past the file not found-error, but instead I get the following:

    CMake Error at /Applications/CLion EAP.app/Contents/bin/cmake/share/cmake-3.1/Modules/FindBoost.cmake:685 (file):

    file STRINGS file "/usr/local/Cellar/boost/1.57.0/boost/version.hpp" cannot be read.

    Call Stack (most recent call first): CMakeLists.txt:11 (find_package)

    Any ideas what I am still missing? The referred line (685) in FindBoost.cmake is: file(STRINGS "${Boost_INCLUDE_DIR}/boost/version.hpp" _boost_VERSION_HPP_CONTENTS REGEX "#define BOOST_(LIB_)?VERSION ")

  • nburk
    nburk over 9 years
    I stumbled upon this question too and it actually worked so that I didn't get the file not found-error anymore... however, I now get a new error: file STRINGS file /usr/local/Cellar/boost/1.57.0/boost/version.hpp" cannot be read.... I updated my question, any ideas?
  • Waxo
    Waxo over 9 years
    Theorically you don't have to set Boost_INCLUDE_DIR and Boost_LIBRARY_DIR, it's the work of the find_package(Boost) line. And don't forget the linking line. If the error still persists maybe have a look at the rights on the file.
  • Waxo
    Waxo over 9 years
    Thank you. It was a pleasure to help, i have hard times when i started CMake, good luck
  • bmeulmeester
    bmeulmeester over 8 years
    Amazing answer, thanks! For OS X users: this will also work when you've installed Boost using Homebrew.
  • irudyak
    irudyak over 8 years
    I've installed boost with MacPorts and it seems that CLion (version 1.2.4) managed to find it automatically (without any changes in CMakeLists.txt).
  • Nguyễn Đức Tâm
    Nguyễn Đức Tâm over 4 years
    Is it the same in Window??