# Including headers from library within same project

**URL:** <https://discourse.cmake.org/t/including-headers-from-library-within-same-project/13496>\
**Category:** Code\
**Created:** [January 27, 2025, 4:55pm UTC](https://discourse.cmake.org/t/including-headers-from-library-within-same-project/13496 "2025-01-27T16:55:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebastian](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sebastian/32/5316_2.png) [@sebastian](https://discourse.cmake.org/u/sebastian)\
**Post date:** [January 27, 2025, 4:55pm UTC](https://discourse.cmake.org/t/including-headers-from-library-within-same-project/13496/1 "2025-01-27T16:55:55Z")

</div>

Hello \*,

I have a project where I am building one application and a bunch of (mostly shared) libraries; the application is set up with `project(myapp C CXX)` and the libraries are set up with `add_library(mod_plugin1 MODULE myplugin1.cpp)` or `add_library(mod_plugin2 STATIC myplugin2.cpp)`. All of the libaries reside in subdirectories with their own CMakeLists.txt which are then included by `add_subdirectory`.

There are several libraries and I do/will not know which of them are present as users may choose freely which ones to compile.

So far, this is all fine and works.

Today, it became clear that one specific, statically linked library (let’s call it “mod\_utils”) is needed by some of the other libraries as well. So I added to the depending libraries (f.e. mod\_plugin1):

```auto
    target_link_libraries(mod_plugin1
            mod_utils
    )

```

This works, but I don’t have access to the libraries header files during **compilation** of mod\_plugin1.

I didn’t find a good solution to that; currently there is a global variable set in the root CMakeLists.txt that holds the include path for mod\_utils and add that variable to `target_include_directories`:

```auto
target_include_system_directories(mod_plugin1
            # ...
            ${mod_utils_inc_path}
    )

```

This variable can’t be set in mod\_utils’ CMakeListst.txt though, since I can not control the order or execution in CMake.

Since this solution does require a change of the main CMakeLists.txt: is there a better way to keep this modular? I looked into `mod_plugin1_SOURCE_DIR` but that’s only available for projects, not for libraries.

Thanks  
Sebastian

---

<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:** [January 28, 2025, 10:40am UTC](https://discourse.cmake.org/t/including-headers-from-library-within-same-project/13496/2 "2025-01-28T10:40:44Z")

</div>

This is a prime use case for CMake’s [usage requirements](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#target-usage-requirements) feature. When you define settings (such as include directories, compilation options, etc.) for a library, you can specify where they apply:

- Apply only when building the library itself (use keyword `PRIVATE`)
- Apply only to consumers of the library, i.e. those who link against the library (use keyword `INTERFACE`)
- Apply to both the library and its consumers (use keyword `PUBLIC`)

In your case, it would look like this:

**CMakeList defining mod\_utils:**

```auto
add_library(mod_utils STATIC ...)
target_include_directories(mod_utils PUBLIC wherever/includes/are)

```

Using `PUBLIC` will ensure that the include directory applies both to the library `mod_utils` and to all who link against it.

**CMakeList defining mod\_plugin1:**

```auto
add_library(mod_plugin1 MODULE ...)
target_link_libraries(mod_plugin1
  mod_utils
)

```

Nothing more needs to be done here, the usage requirements will be propagated by CMake automatically.

---

<div class="post-metadata">

**Author:** ![sebastian](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sebastian/32/5316_2.png) [@sebastian](https://discourse.cmake.org/u/sebastian)\
**Post date:** [January 28, 2025, 3:16pm UTC](https://discourse.cmake.org/t/including-headers-from-library-within-same-project/13496/3 "2025-01-28T15:16:57Z")

</div>

Oh … ooooooooh! I completely forgot about the possibility to use `PUBLIC` in `target_include_directories`! That makes things so much easier and cleaner … 🙂

Thanks a **lot** ,  
Sebastian
