code_by_cr
code_by_cr

Reputation: 23

include boost in CMake

I try to include the current version of the Boost library into my cmake file. But I 'm struggle with the full include. I have already included the boost::filesystem library. So I hope this is not a big change in the code.

Here my Cmake include that works for me:

#include boost
#===========================================================================
set(Boost_INCLUDE_DIR "/Volumes/Code/boost_1_79_0")
set(Boost_LIBRARY_DIR "/Volumes/Code/boost_1_79_0/stage/lib")

find_package(Boost 
    1.79.0 REQUIRED 
    COMPONENTS 
        filesystem
    )

if(NOT Boost_FOUND)
    message(FATAL_ERROR "Boost Not found")
else()
    message(STATUS "Boost version ${Boost_VERSION} found")
endif()

include_directories(include)
include_directories(${Boost_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE} 
PUBLIC  
    Boost::filesystem)
#===========================================================================

Then I tried that and it failed:

#include boost
#===========================================================================
set(Boost_INCLUDE_DIR "/Volumes/Code/boost_1_79_0")
set(Boost_LIBRARY_DIR "/Volumes/Code/boost_1_79_0/stage/lib")

find_package(Boost 
    1.79.0 REQUIRED 
    COMPONENTS 
        filesystem
        optional
    )

if(NOT Boost_FOUND)
    message(FATAL_ERROR "Boost Not found")
else()
    message(STATUS "Boost version ${Boost_VERSION} found")
endif()

include_directories(include)
include_directories(${Boost_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE} 
PUBLIC  
    Boost::filesystem
    Boost::optional) 
#===========================================================================

Does anyone know a way to integrate all Boost Libraries without listing them or sees my mistake? If someone has suggestions for improvements, feel free to teaching me.

Upvotes: 0

Views: 4223

Answers (1)

Marcus Müller
Marcus Müller

Reputation: 36462

set(Boost_INCLUDE_DIR "/Volumes/Code/boost_1_79_0")
set(Boost_LIBRARY_DIR "/Volumes/Code/boost_1_79_0/stage/lib")

That's not how you should use CMake: your solution just hardcodes paths, and the whole idea of CMake is that you can take a CMake project and build it on a different machine, where paths and necessary flags are different.

So, delete that altogether. You want to have

set(BOOST_REQUIRED_COMPONENTS
    filesystem
    optional
)
set(BOOST_MIN_VERSION 1.79.0) # or whatever you need!
# Here you can either append or set a root to look into
# In general, **don't do that**! Your boost build and installation
# should come with the correct CMake configure scripts to automate this!
# If you set it here, the people trying to build your code on a different
# machine **WILL** hate you.
# set(BOOST_ROOT "/Volumes/Code/boost_1_79_0")
find_package(
    Boost ${BOOST_MIN_VERSION} REQUIRED
    COMPONENTS ${BOOST_REQUIRED_COMPONENTS}
)
target_link_libraries(${EXECUTABLE} PUBLIC  
    Boost::filesystem
    Boost::optional
)
target_include_directories(${EXECUTABLE} PRIVATE include)

You whole setting include_directories(${Boost_INCLUDE_DIR}) is redundant (and a bad idea); target_link_libraries( ... Boost::...) does that automatically.

Does anyone know a way to integrate all Boost Libraries without listing them or sees my mistake?

You don't do that, or your linking takes forever, you produce, under circumstances, gigantic object files and executables, and all for the advantage of not having to write down each Boost component as you use them, in a single place!

So, you could use the ALL meta-compoent (which was introduced by Boost 1.73), but I'd strongly encourage you not to do that.

Upvotes: 3

Related Questions