What's the best practice to import external boost in a c++ cmake project?

Viewed 282

I'd like to import external boost in a c++ cmake project.

I did several tries and finally got the following CMakeLists.txt works:

cmake_minimum_required(VERSION 3.14)
project(my_project
    VERSION 0.0.1
    LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 11)

include(FetchContent)
FetchContent_Declare(
    boost
    URL https://boostorg.jfrog.io/artifactory/main/release/1.78.0/source/boost_1_78_0.tar.bz2
)
FetchContent_MakeAvailable(boost)

set(Boost_ROOT "${CMAKE_BINARY_DIR}/_deps/boost-src")
set(BOOST_INCLUDEDIR ${Boost_ROOT})

find_package(Boost 1.78)
include_directories(${Boost_INCLUDE_DIRS})

add_executable(
    boost_test
    tests/boost_test.cpp)

However, I feel a little uncomfortable because set(Boost_ROOT "${CMAKE_BINARY_DIR}/_deps/boost-src") looks hacky -- it has a strong assumption that the fetched content will be stored in _deps folder, which I rather want to put it in a dedicated folder like 3rd_party or external as typical C++ project layout conventions.

I found there're 2 ways for doing that:

  1. Use FetchContent_Declare to download the project. However, I don't know how to execute extra build commands to compile non-header-only libraries.
  2. Use ExternalProject_Add to download and compile the boost project. It looks like an old-fashing way, besides, I don't know how to add boost as part of dependencies to top-level CMakeLists.txt in this way.

The way I adopted is the former one, but I wonder if it's the correct way to include external boost (not system-default installed one) as part of a C++ project, or is there a best practice for doing this?

Any comments, suggestions, answers are welcome. Thanks.

0 Answers
Related