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?
boost::filesystem
will no longer be receiving updates as it has been absorbed into C++17. – Sightreadgit pull -r && git submodule update --recursive && ./bootstrap.sh --with-libraries=all ...
– Keownfind_package(Boost)
and vcpkg – Rheostatgit clone --recurse-submodules https://github.com/boostorg/boost
approach. 99% of the time it will be quicker to download the release tarball – Scatterfind_package(Boost)
- not working if you need exact version of library or add patch to it. – Himyaritic