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?
This is what I used:
cmake_minimum_required(VERSION 3.24)
project(p)
set(BOOST_INCLUDE_LIBRARIES thread filesystem system program_options)
set(BOOST_ENABLE_CMAKE ON)
include(FetchContent)
FetchContent_Declare(
Boost
GIT_REPOSITORY https://github.com/boostorg/boost.git
GIT_TAG boost-1.80.0
GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(Boost)
add_executable(boost_test boost_test.cpp)
target_link_libraries(boost_test PRIVATE Boost::filesystem
Boost::program_options)
This is not exactly clear why, but in your case you can reduce version to:
cmake_minimum_required(VERSION 3.24)
project(p)
set(BOOST_INCLUDE_LIBRARIES thread filesystem system)
set(BOOST_ENABLE_CMAKE ON)
include(FetchContent)
FetchContent_Declare(
Boost
GIT_REPOSITORY https://github.com/boostorg/boost.git
GIT_TAG boost-1.74.0
GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(Boost)
add_executable(boost_test boost_test.cpp)
target_link_libraries(boost_test PRIVATE Boost::filesystem)
See also:
It's also possible to download a zip archive from a boost release on GitHub. This will be the fastest option for download (considering the size of ~90 MB) and you can be sure that you definitively have all sources / no dependencies are missing.
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 14)
# Add boost lib sources
set(BOOST_INCLUDE_LIBRARIES thread filesystem system)
set(BOOST_ENABLE_CMAKE ON)
# Download and extract the boost library from GitHub
message(STATUS "Downloading and extracting boost library sources. This will take some time...")
include(FetchContent)
Set(FETCHCONTENT_QUIET FALSE) # Needed to print downloading progress
FetchContent_Declare(
Boost
URL https://github.com/boostorg/boost/releases/download/boost-1.84.0/boost-1.84.0.7z # downloading a zip release speeds up the download
USES_TERMINAL_DOWNLOAD TRUE
GIT_PROGRESS TRUE
DOWNLOAD_NO_EXTRACT FALSE
)
FetchContent_MakeAvailable(Boost)
# Test
add_executable(test test.cpp)
target_link_libraries(test PRIVATE Boost::filesystem)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With