# FetchContent with Boost

**URL:** <https://discourse.cmake.org/t/fetchcontent-with-boost/6596>\
**Category:** Code\
**Tags:** os:windows\
**Created:** [October 4, 2022, 2:18pm UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596 "2022-10-04T14:18:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![malaterre](https://discourse.cmake.org/user_avatar/discourse.cmake.org/malaterre/32/1971_2.png) [@malaterre](https://discourse.cmake.org/u/malaterre)\
**Post date:** [October 4, 2022, 2:18pm UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/1 "2022-10-04T14:18:27Z")

</div>

Hi there,

I have been trying to make sense of the FetchContent documentation at:

- [https://cmake.org/cmake/help/latest/module/FetchContent.html](https://cmake.org/cmake/help/latest/module/FetchContent.html)

However I must say I totally fail to understand the documentation in my simple case:

```auto
cmake_minimum_required(VERSION 3.24)
project(p)

set(BOOST_INCLUDE_LIBRARIES math 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
)
FetchContent_MakeAvailable(Boost)

add_executable(boost_test boost_test.cpp)
target_link_libraries(boost_test PRIVATE Boost::filesystem
                                         Boost::program_options)

```

If I compile my code I get:

```auto
boost_test.cpp:2:10: fatal error: boost/math/special_functions/round.hpp: No such file or directory
    2 | #include <boost/math/special_functions/round.hpp>
      | ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

```

For some reason `FetchContent_MakeAvailable` did construct an almost working set of include directories, but for some reason did not add the math directories.

What did I misunderstood from FetchContent documentation ?

Thanks

---

<div class="post-metadata">

**Author:** ![xhustango](https://discourse.cmake.org/user_avatar/discourse.cmake.org/xhustango/32/1614_2.png) [@xhustango](https://discourse.cmake.org/u/xhustango)\
**Post date:** [October 4, 2022, 3:42pm UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/2 "2022-10-04T15:42:23Z")

</div>

Hi there,

Seeing the code it seems you are correctly calling boost and the modules you want. In the _target\_link\_libraries_ I think you miss to add _Boost::math_, you are linking just to _filesystem_ and _program\_options_.

---

<div class="post-metadata">

**Author:** ![malaterre](https://discourse.cmake.org/user_avatar/discourse.cmake.org/malaterre/32/1971_2.png) [@malaterre](https://discourse.cmake.org/u/malaterre)\
**Post date:** [October 5, 2022, 7:13am UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/3 "2022-10-05T07:13:30Z")

</div>

I was hoping to get a more detailed answer in this case. The correct solution is indeed

`target_link_libraries(boost_test PRIVATE Boost::math ...)`

But when I read the documentation for target\_link\_libraries, here is what I see:

> Specify libraries or flags to use when linking a given target and/or its dependents.

Could someone explain why target\_link\_libraries would have an influence `target_include_directories` ?

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [October 5, 2022, 7:19am UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/4 "2022-10-05T07:19:26Z")

</div>

That’s because in CMake targets carry their dependencies when linking. `Boost::math` is a target, so it carries the information about needed defines, location of the libraries, and needed include directories. Those pieces of information will be added to your target when using `target_link_libraries`

---

<div class="post-metadata">

**Author:** ![malaterre](https://discourse.cmake.org/user_avatar/discourse.cmake.org/malaterre/32/1971_2.png) [@malaterre](https://discourse.cmake.org/u/malaterre)\
**Post date:** [October 5, 2022, 7:30am UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/5 "2022-10-05T07:30:36Z")

</div>

Thanks for the quick answer. Here is the documentation I am reading:

> Each `<item>` may be:
> 
> - **A library target name** : The generated link line will have the full path to the linkable library file associated with the target. The buildsystem will have a dependency to re-link `<target>` if the library file changes.

ref:

- [https://cmake.org/cmake/help/latest/command/target\_link\_libraries.html](https://cmake.org/cmake/help/latest/command/target_link_libraries.html)

Would you mind sharing a link to the cmake documentation where you found this hint ? Thanks much

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [October 5, 2022, 11:03am UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/6 "2022-10-05T11:03:04Z")

</div>

I’d love to give u a link, but I cannot find it either. It is one of the things that I have organically learned by using CMake and reading answers on SO, and MailingList.  
If it’s written in the documentation in clear text, someone more familiar with the docu would have to point to it. Sorry.

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [October 5, 2022, 11:08am UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/7 "2022-10-05T11:08:22Z")

</div>

[Chapter 3 of the tutorial](https://cmake.org/cmake/help/latest/guide/tutorial/Adding%20Usage%20Requirements%20for%20a%20Library.html) might be a good starting point.

---

<div class="post-metadata">

**Author:** ![malaterre](https://discourse.cmake.org/user_avatar/discourse.cmake.org/malaterre/32/1971_2.png) [@malaterre](https://discourse.cmake.org/u/malaterre)\
**Post date:** [October 5, 2022, 11:34am UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/8 "2022-10-05T11:34:12Z")

</div>

I was not expecting to get that much attention for my silly issue 😃

So given the interest, could someone please confirm what is the expected behavior of `FetchContent` for another cmake based project.

```auto
cmake_minimum_required(VERSION 3.24)
project(p)

include(FetchContent)
FetchContent_Declare(
  Log4CPP
  URL https://sourceforge.net/projects/log4cpp/files/log4cpp-1.1.x%20%28new%29/log4cpp-1.1/log4cpp-1.1.3.tar.gz
  DOWNLOAD_EXTRACT_TIMESTAMP TRUE
  )
FetchContent_MakeAvailable(Log4CPP)
if(TARGET log4cpp)
        message("I am a target")
endif()
add_executable(main main.cpp)
target_link_libraries(main PRIVATE log4cpp)

```

Short answer: the include\_directory is not setup as in the previous example.

So where is the issue: in my code ? in cmake/FetchContent ? In log4cpp cmake project ?

Thanks

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [October 7, 2022, 5:04am UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/9 "2022-10-07T05:04:48Z")

</div>

None of this is really specific to FetchContent. Your queries would be the same if you simply downloaded Boost sources yourself or added Boost as a git submodule, then brought those sources into your build with `add_subdirectory()`. The same applies to any other project you might choose to absorb into your build.

So to directly answer your last question, the expected behavior is: if `log4cpp` is a target that is being defined by the sources you’re downloading and absorbing into your build, then it should work fine as long as that target correctly adds its usage requirements. It would usually do that using things like `target_include_directories()`, `target_compile_definitions()`, etc. If the build doesn’t work, I would be looking into the log4cpp project to see why. Not all projects expect to be absorbed into a larger parent build this way, so you can’t always use FetchContent, git submodules, etc. to bring every project into your own.

Excuse the brevity, I’m answering queries between other obligations. 😉

---

<div class="post-metadata">

**Author:** ![melroy89](https://discourse.cmake.org/user_avatar/discourse.cmake.org/melroy89/32/4091_2.png) [@melroy89](https://discourse.cmake.org/u/melroy89)\
**Post date:** [December 22, 2023, 1:04am UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/10 "2023-12-22T01:04:14Z")

</div>

I know it’s a old thread, but I want to share this knowledge anyway with the world.

The fastest way now to download boost is to download the artifact in compressed format, instead of cloning the git repository with FetchContent, see below:

```auto
set(BOOST_INCLUDE_LIBRARIES asio regex algorithm)
set(BOOST_ENABLE_CMAKE ON)
FetchContent_Declare(
  Boost
  URL https://github.com/boostorg/boost/releases/download/boost-1.84.0/boost-1.84.0.7z
  USES_TERMINAL_DOWNLOAD TRUE
  DOWNLOAD_NO_EXTRACT FALSE
)
FetchContent_MakeAvailable(Boost)

```

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [December 23, 2023, 9:38pm UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/11 "2023-12-23T21:38:41Z")

</div>

Is there a usable `CMakeLists.txt` include in the boost tar archive?

---

<div class="post-metadata">

**Author:** ![melroy89](https://discourse.cmake.org/user_avatar/discourse.cmake.org/melroy89/32/4091_2.png) [@melroy89](https://discourse.cmake.org/u/melroy89)\
**Post date:** [December 23, 2023, 10:00pm UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/12 "2023-12-23T22:00:08Z")

</div>

Yea there is. I now switched from 7z to tar.xz release archive. Works fine!

You can also add URL\_MD5 with the md5sum hash, so you are sure you download the correct file also in the future. I now have the following:

```auto
set(BOOST_INCLUDE_LIBRARIES asio regex algorithm)
set(BOOST_ENABLE_CMAKE ON)
FetchContent_Declare(
  Boost
  URL https://github.com/boostorg/boost/releases/download/boost-1.84.0/boost-1.84.0.tar.xz
  URL_MD5 893b5203b862eb9bbd08553e24ff146a
)
FetchContent_MakeAvailable(Boost)

```

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [December 27, 2023, 9:09am UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/13 "2023-12-27T09:09:14Z")

</div>

Do you know [Why you should NOT use Boost git repo with CPM.cmake](https://github.com/cpm-cmake/CPM.cmake/discussions/448) or FetchContents

It works, but you can’t install any boost library, and so you can’t install your own library that is depending on a boost library!

---

<div class="post-metadata">

**Author:** ![melroy89](https://discourse.cmake.org/user_avatar/discourse.cmake.org/melroy89/32/4091_2.png) [@melroy89](https://discourse.cmake.org/u/melroy89)\
**Post date:** [December 27, 2023, 3:54pm UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/14 "2023-12-27T15:54:09Z")

</div>

Did you also link boost to your binary? Like so:

```auto
target_link_libraries(your_target Boost::asio);

```

Of course you also need to include the boost header as well in your code.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [December 27, 2023, 7:38pm UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/15 "2023-12-27T19:38:53Z")

</div>

```bash
Claus-iMac:netkit-tftp clausklein$ cmake --workflow --preset default
Executing workflow step 1 of 5: configure preset "default"

Preset CMake variables:

  BUILD_SHARED_LIBS:BOOL="TRUE"
  CMAKE_BUILD_TYPE="Release"
  CMAKE_CXX_STANDARD="20"
  CMAKE_DEBUG_POSTFIX="D"
  CMAKE_INSTALL_PREFIX:PATH="/Users/clausklein/Workspace/cpp/netkit-tftp/stagedir"
  CMAKE_PREFIX_PATH:STRING="/Users/clausklein/Workspace/cpp/netkit-tftp/stagedir"

Preset environment variables:

  CMAKE_EXPORT_COMPILE_COMMANDS="YES"
  CPM_SOURCE_CACHE="/Users/clausklein/.cache/CPM"
  CPM_USE_LOCAL_PACKAGES="NO"
  PATH="/Users/clausklein/.local/bin:/usr/local/opt/net-snmp/bin:/Users/clausklein/Library/Python/3.9/bin:/usr/local/opt/python/libexec/bin:/usr/local/opt/sqlite/bin:/usr/local/bin:/usr/local/sbin:/Users/clausklein/scripts:/Users/clausklein/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/texbin:/usr/local/MacGPG2/bin:/opt/X11/bin:/Library/Apple/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin"

-- Boost: Release build, shared libraries, MPI OFF, Python OFF, testing OFF
-- Boost: libraries included: asio;filesystem
CMake Warning (dev) at build/_deps/boost-src/tools/cmake/include/BoostRoot.cmake:308 (set):
  uninitialized variable 'CMAKE_FOLDER'
Call Stack (most recent call first):
  build/_deps/boost-src/CMakeLists.txt:20 (include)
This warning is for project developers. Use -Wno-dev to suppress it.

CMake Warning (dev) at build/_deps/boost-src/tools/cmake/include/BoostRoot.cmake:310 (if):
  uninitialized variable 'CMAKE_FOLDER'
Call Stack (most recent call first):
  build/_deps/boost-src/CMakeLists.txt:20 (include)
This warning is for project developers. Use -Wno-dev to suppress it.

-- Boost.Context: architecture x86_64, binary format mach-o, ABI sysv, assembler gas, suffix .S, implementation fcontext
-- CPM: Adding package PackageProject.cmake@1.11.0 (v1.11.0 at /Users/clausklein/.cache/CPM/packageproject.cmake/fc402989ee6e628476b3d8661f8f96774d89df74)
-- CPM: Adding package project_options@0.32.1 (v0.32.1 at /Users/clausklein/.cache/CPM/project_options/74a42772cc49148bbbf34bad0ffe51f95b47cf3f)
-- Configuring done (0.9s)
CMake Error: install(EXPORT "tftpdTargets" ...) includes target "tftpd" which requires target "boost_filesystem" that is not in any export set.
-- Generating done (0.1s)
CMake Generate step failed. Build files cannot be regenerated correctly.
Claus-iMac:netkit-tftp clausklein$ 

```

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [December 27, 2023, 7:41pm UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/16 "2023-12-27T19:41:48Z")

</div>

@ClausKlein While Boost itself might not define `install(...)` rules for you, that doesn’t stop you adding your own `install(...)` commands for Boost targets in your own project. Starting with CMake 3.13, you can put `install(TARGETS ...)` commands in a different directory scope to the one that defines those targets.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [December 27, 2023, 7:43pm UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/17 "2023-12-27T19:43:05Z")

</div>

> [@melroy89](#):
>
> Of course you also need to include the boost header as well in your code.

```diff
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2fd501f..f99e70f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -26,11 +26,23 @@ include(GNUInstallDirs)
 # search required packages and libs
 #---------------------------------------------------------------------------------------
 
-find_package(
- Boost 1.71 CONFIG
- COMPONENTS filesystem
- REQUIRED
+# find_package(
+# Boost 1.84 CONFIG
+# COMPONENTS filesystem
+# REQUIRED
+# )
+
+include(FetchContent)
+
+set(BOOST_INCLUDE_LIBRARIES asio filesystem)
+option(BOOST_SKIP_INSTALL_RULES NO)
+FetchContent_Declare(
+ Boost
+ URL https://github.com/boostorg/boost/releases/download/boost-1.84.0/boost-1.84.0.tar.xz
+ URL_MD5 893b5203b862eb9bbd08553e24ff146a
 )
+FetchContent_MakeAvailable(Boost)
+
 
 # ---- Add other dependencies via CPM ----
 # see https://github.com/cpm-cmake/CPM.cmake for more info
@@ -100,7 +112,7 @@ if(NOT CMAKE_SKIP_INSTALL_RULES)
         # EXPORT_HEADER ${EXPORT_HEADER_LOCATION}
         COMPATIBILITY SameMajorVersion
         DISABLE_VERSION_SUFFIX YES
- DEPENDENCIES "boost 1.71"
+ DEPENDENCIES "Boost 1.84"
     )
     # NOTE: implicit done! add_library(tftpd::tftpd ALIAS tftpd)
     install(FILES async_tftpd_server.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

```

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [December 27, 2023, 7:46pm UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/18 "2023-12-27T19:46:10Z")

</div>

> [@ClausKlein](#):
>
> `option(BOOST_SKIP_INSTALL_RULES NO)`

This line looks malformed, but it will happen to work by chance. The syntax is:

```cmake
option(<varName> <helpString> [<value>])

```

In your case, you set the `<helpString>` to `NO`. It just so happens that when you omit the `<value>` it defaults to false anyway.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [December 27, 2023, 7:53pm UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/19 "2023-12-27T19:53:56Z")

</div>

> [@craig.scott](#):
>
> Starting with CMake 3.13, you can put `install(TARGETS ...)` commands in a different directory scope to the one that defines those targets.

**Boost cmake system is usable for developers only!**

```bash
CMake Error at CMakeLists.txt:100 (install):
  install TARGETS given target "filesystem" which does not exist.

-- Configuring incomplete, errors occurred!

```

---

<div class="post-metadata">

**Author:** ![melroy89](https://discourse.cmake.org/user_avatar/discourse.cmake.org/melroy89/32/4091_2.png) [@melroy89](https://discourse.cmake.org/u/melroy89)\
**Post date:** [December 27, 2023, 7:56pm UTC](https://discourse.cmake.org/t/fetchcontent-with-boost/6596/20 "2023-12-27T19:56:28Z")

</div>

Find package is not required anymore since you use fetchcontent to retrieve the boost dependency.

[Next page](https://discourse.cmake.org/t/fetchcontent-with-boost/6596.md?page=2)
