c++boostcmakebuilddependency-management

How to use Boost libraries directly from github using cmake FetchContent or any simpler solution?


Let's assume we would like to use boost::file_system library in our cmake multiplatform project (ios, macos, android, windows, linux). One way to do it is to directly copy boost source code into our project. It increases project size and add a lot of maintenance problems, patching, updating and etc. What if we download boost sources during cmake configure step. So I add minimal example (file - main.cxx):

    #include <boost/filesystem.hpp>

    #include <iostream>

    int main(int, char**)
    {
        std::cout << boost::filesystem::current_path() << std::endl;
        return std::cout.fail();
    }

Next is full CMakeLists.txt file to build this minimal example from source without installing boost into system.

cmake_minimum_required(VERSION 3.20...3.23)

project(19-boost-file-system CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
set(CMAKE_ENABLE_EXPORTS OFF)

include(FetchContent)

fetchcontent_declare(
    BoostAssert GIT_REPOSITORY https://github.com/boostorg/assert.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostConfig GIT_REPOSITORY https://github.com/boostorg/config.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostContainerHash
    GIT_REPOSITORY https://github.com/boostorg/container_hash.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostCore GIT_REPOSITORY https://github.com/boostorg/core.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostDetail GIT_REPOSITORY https://github.com/boostorg/detail.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostInteger GIT_REPOSITORY https://github.com/boostorg/integer.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostStaticAssert
    GIT_REPOSITORY https://github.com/boostorg/static_assert.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostThrowException
    GIT_REPOSITORY https://github.com/boostorg/throw_exception.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostTypeTraits GIT_REPOSITORY https://github.com/boostorg/type_traits.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostPreprocessor
    GIT_REPOSITORY https://github.com/boostorg/preprocessor.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostIterator GIT_REPOSITORY https://github.com/boostorg/iterator.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(BoostIo GIT_REPOSITORY https://github.com/boostorg/io.git
                     GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostConceptCheck
    GIT_REPOSITORY https://github.com/boostorg/concept_check.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostConversion GIT_REPOSITORY https://github.com/boostorg/conversion.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostFunctionTypes
    GIT_REPOSITORY https://github.com/boostorg/function_types.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostFusion GIT_REPOSITORY https://github.com/boostorg/fusion.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(BoostMpl GIT_REPOSITORY https://github.com/boostorg/mpl.git
                     GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostOptional GIT_REPOSITORY https://github.com/boostorg/optional.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostSmartPtr GIT_REPOSITORY https://github.com/boostorg/smart_ptr.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostUtility GIT_REPOSITORY https://github.com/boostorg/utility.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostTypeof GIT_REPOSITORY https://github.com/boostorg/typeof.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostTuple GIT_REPOSITORY https://github.com/boostorg/tuple.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostPredef GIT_REPOSITORY https://github.com/boostorg/predef.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostMove GIT_REPOSITORY https://github.com/boostorg/move.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostAtomic GIT_REPOSITORY https://github.com/boostorg/atomic.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostVariant2 GIT_REPOSITORY https://github.com/boostorg/variant2.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostAlign GIT_REPOSITORY https://github.com/boostorg/align.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostMp11 GIT_REPOSITORY https://github.com/boostorg/mp11.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostWinapi GIT_REPOSITORY https://github.com/boostorg/winapi.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostSystem GIT_REPOSITORY https://github.com/boostorg/system.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostFileSystem GIT_REPOSITORY https://github.com/boostorg/filesystem.git
    GIT_TAG boost-1.79.0)

fetchcontent_makeavailable(
    BoostAssert
    BoostConfig
    BoostInteger
    BoostStaticAssert
    BoostThrowException
    BoostTypeTraits
    BoostPreprocessor
    BoostIo
    BoostIterator
    BoostConceptCheck
    BoostConversion
    BoostFunctionTypes
    BoostFusion
    BoostMpl
    BoostOptional
    BoostSmartPtr
    BoostUtility
    BoostTypeof
    BoostTuple
    BoostPredef
    BoostMove
    BoostAlign
    BoostMp11
    BoostWinapi
    BoostContainerHash
    BoostCore
    BoostDetail
    BoostAtomic
    BoostVariant2
    BoostSystem
    BoostFileSystem)

add_executable(main_boost main.cxx)
target_link_libraries(main_boost PRIVATE Boost::filesystem)

May be you know more simpler solution? How can one compile and link with Boost libs without any hussle directly from GitHub using cmake?


Solution

  • Today I see correct and minimal example on how to use boost from github. I play with it and everything seems to be fine. You do not need to use any fancy variables with library names. It woun't help to speed up download Boost. Better use find_package and cmake will find all dependencies for you.

    cmake_minimum_required(VERSION 3.27...3.29)
    
    project(19-boost-file-system CXX)
    
    include(FetchContent)
    
    # The logging output during population can be quite verbose,
    # making the configure stage quite noisy. This cache option
    # (ON by default) hides all population output unless an error is encountered.
    # If experiencing problems with hung downloads, temporarily
    # switching this option off may help diagnose which content population
    # is causing the issue.
    set(FETCHCONTENT_QUIET OFF)
    
    fetchcontent_declare(
        Boost
        GIT_REPOSITORY "https://github.com/boostorg/boost.git"
        GIT_TAG "boost-1.85.0"
        GIT_PROGRESS ON
        GIT_SHALLOW ON
        OVERRIDE_FIND_PACKAGE TRUE # needed to find correct Boost
        EXCLUDE_FROM_ALL # compile only what you need)
    
    fetchcontent_makeavailable(Boost)
    
    find_package(
        Boost
        1.86.0
        EXACT # Minimum or EXACT version e.g. 1.67.0
        REQUIRED # Fail with error if Boost is not found
        COMPONENTS filesystem # Boost libraries by their canonical name
        # e.g. "date_time" for "libboost_date_time"
        #[OPTIONAL_COMPONENTS <libs>...]
        # Optional Boost libraries by their canonical name)
        ) # e.g. "date_time" for "libboost_date_time"
    
    
    add_executable(main_boost main.cxx)
    
    target_link_libraries(main_boost PRIVATE Boost::filesystem)