0

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.

code_by_cr
  • 21
  • 6
  • cmake_minimum_required(VERSION 3.19...3.23.2) – code_by_cr Jun 17 '22 at 14:33
  • 2
    Optional is a header only library so i think shouldn't be specified in components – Alan Birtles Jun 17 '22 at 14:47
  • You don't need anything more to use boost optional if you're using boost filesystem, simply keep your working code or adapt the answer below for a cleaner code. – Holt Jun 17 '22 at 14:54
  • Just in case you need to load just header only libraries a `find_package(Boost REQUIRED)` and later `target_link_libraries(${EXECUTABLE} PUBLIC Boost::boost)` or since CMake 3.15 `target_link_libraries(${EXECUTABLE} PUBLIC Boost::headers)` comes handy. To find out which of the Boost libraries are header only see [this question](https://stackoverflow.com/questions/13604090/which-boost-libraries-are-header-only) – vre Jun 17 '22 at 15:27
  • Boost 1.79 requires CMake 3.23.2, see https://stackoverflow.com/a/42124857/2799037 – usr1234567 Jun 17 '22 at 19:28

1 Answers1

2
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.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • While those are nice advice to mix CMake/Boost, this does not address the original problem at all. – Holt Jun 17 '22 at 14:55
  • ah, sorry, I had that trick somewhere, but I wrote down the scaffolding around it first. Then forgot it. Give me a second. – Marcus Müller Jun 17 '22 at 14:56
  • @MarcusMüller thanks for your answer. I have the project and boost on external partitions so I thought I would need links to them. – code_by_cr Jun 17 '22 at 15:30
  • Well, you do need to tell CMake how to find it. Usually, that's done by you *installing* the development library (and that should be pretty painless), so that CMake looks for it in the right places. If you can't do that for some reason, using `cmake -DBOOST_ROOT=/path/to/Boost/ …` would be more appropriate. – Marcus Müller Jun 17 '22 at 16:03