Search code examples
boostcmake

How do you add Boost libraries in CMakeLists.txt?


I need to add Boost libraries into my CMakeLists.txt. How do you do it or how do you add it?


Solution

  • Put this in your CMakeLists.txt file (change any options from OFF to ON if you want):

    set(Boost_USE_STATIC_LIBS OFF) 
    set(Boost_USE_MULTITHREADED ON)  
    set(Boost_USE_STATIC_RUNTIME OFF) 
    find_package(Boost 1.45.0 COMPONENTS *boost libraries here*) 
    
    if(Boost_FOUND)
        include_directories(${Boost_INCLUDE_DIRS}) 
        add_executable(progname file1.cxx file2.cxx) 
        target_link_libraries(progname ${Boost_LIBRARIES})
    endif()
    

    Obviously you need to put the libraries you want where I put *boost libraries here*. For example, if you're using the filesystem and regex library you'd write:

    find_package(Boost 1.45.0 COMPONENTS filesystem regex)