# What's the best way to include headers from FetchContent deps?

**URL:** https://discourse.cmake.org/t/whats-the-best-way-to-include-headers-from-fetchcontent-deps/7047
**Category:** Code
**Created:** [December 12, 2022, 1:04am UTC](https://discourse.cmake.org/t/whats-the-best-way-to-include-headers-from-fetchcontent-deps/7047 "2022-12-12T01:04:31Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Chrinkus](https://discourse.cmake.org/user_avatar/discourse.cmake.org/chrinkus/32/1801_2.png) [@Chrinkus](https://discourse.cmake.org/u/Chrinkus)
#### Post date: [December 12, 2022, 1:04am UTC](https://discourse.cmake.org/t/whats-the-best-way-to-include-headers-from-fetchcontent-deps/7047/1 "2022-12-12T01:04:31Z")

</div>

I’ve added the bare-bones FetchContent statements to my project that declare and make available some support libraries that I’ve also written. I’ve been successfully compiling and running from my source tree but didn’t realize that I was still including the headers that I had installed to my system before learning how to fetch dependencies.

```auto
cmake_minimum_required(VERSION 3.22)

project(aoc-2022 VERSION 0.1 LANGUAGES C)

set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED YES)

# Support Libraries
include(FetchContent)
FetchContent_Declare(cgs
        GIT_REPOSITORY https://github.com/Chrinkus/libcgs.git
        GIT_TAG main
)
FetchContent_Declare(fruity
        GIT_REPOSITORY https://github.com/Chrinkus/fruity.git
        GIT_TAG main
)
FetchContent_MakeAvailable(cgs fruity)

# snip

```

So now that I’ve deleted the previously installed libs and am trying to run from my build dir, my include statements are failing. Is there a better, more idiomatic way to resolve include paths than adding:

```auto
include_directories(_deps/cgs-src/src)

```

Perhaps using some defined variables like ‘CGS\_INCLUDE’ or something?

The repo with the full file and links to the libs in the README is [here](https://github.com/Chrinkus/advent-of-code/blob/main/2022/c/CMakeLists.txt).

---

<div class="post-metadata">

### Author: ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)
#### Post date: [December 12, 2022, 1:21am UTC](https://discourse.cmake.org/t/whats-the-best-way-to-include-headers-from-fetchcontent-deps/7047/2 "2022-12-12T01:21:15Z")

</div>

Just use targets. Your support libraries should have targets that you use like:

```cmake
target_link_libraries (aoc-2022 PRIVATE cgs fruity)

```

and that should be it. The `cgs` and `fruity` targets should set their own include directories, which propagate to targets that link against them.

---

<div class="post-metadata">

### Author: ![Chrinkus](https://discourse.cmake.org/user_avatar/discourse.cmake.org/chrinkus/32/1801_2.png) [@Chrinkus](https://discourse.cmake.org/u/Chrinkus)
#### Post date: [December 12, 2022, 1:32am UTC](https://discourse.cmake.org/t/whats-the-best-way-to-include-headers-from-fetchcontent-deps/7047/3 "2022-12-12T01:32:26Z")

</div>

The libraries are linking fine, I just can’t include the headers. I also have written the support libraries and I’m not sure how to export include directories. I have declared some Public Headers in libcgs but I’ve gone through all of the build output and can’t find any variables set.

This has been an issue I’ve had for a while but I’ve always just adjusted my path to fix it. Now I have some people trying to build my code and I don’t want them to have to muck around. So how do I make my support libraries more consumable?

---

<div class="post-metadata">

### Author: ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)
#### Post date: [December 12, 2022, 1:42am UTC](https://discourse.cmake.org/t/whats-the-best-way-to-include-headers-from-fetchcontent-deps/7047/4 "2022-12-12T01:42:01Z")

</div>

The simplest way to do this is like this:

```cmake
add_library (cgs)

target_include_directories (cgs PUBLIC "${CMAKE_CURRENT_LIST_DIR}/include") # or whatever path

```

with that code, any target who links against the `cgs` library using `target_link_libraries` will get that include directory transitively.

Often you need to differentiate between include directories when building vs when installing, for example if you have generated files. In that case you do something like:

```cmake
add_library (cgs)

include (GNUInstallDirs)

target_include_directories (cgs 
    PUBLIC
        "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>" # these dirs are only used when building inside the build tree
        "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>" # these dirs are only used when linking against a prebuilt version of your package
)

```

---

<div class="post-metadata">

### Author: ![Chrinkus](https://discourse.cmake.org/user_avatar/discourse.cmake.org/chrinkus/32/1801_2.png) [@Chrinkus](https://discourse.cmake.org/u/Chrinkus)
#### Post date: [December 12, 2022, 6:03am UTC](https://discourse.cmake.org/t/whats-the-best-way-to-include-headers-from-fetchcontent-deps/7047/5 "2022-12-12T06:03:42Z")

</div>

This made sense as soon as I read it. Thank you for your time and advice!

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [December 12, 2022, 5:26pm UTC](https://discourse.cmake.org/t/whats-the-best-way-to-include-headers-from-fetchcontent-deps/7047/6 "2022-12-12T17:26:01Z")

</div>

> [@Chrinkus](#):
>
> `GIT_TAG main`

I suggest using `origin/main` so that it updates to the remote’s `main` instead of “freezing” at whatever your local `main` is on the first clone.
