# Populate include directories from sub\_directory to all of parts of the project. How?

**URL:** https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449
**Category:** Code
**Created:** [July 26, 2025, 12:48pm UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449 "2025-07-26T12:48:17Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![alexandr](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@alexandr](https://discourse.cmake.org/u/alexandr)
#### Post date: [July 26, 2025, 12:48pm UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/1 "2025-07-26T12:48:17Z")

</div>

Hello all!

My project have this structure:

Application (add\_executable)  
| – Library 1 (add\_subdirectory in application and add\_library in library)  
| – Library 2 (add\_subdirectory in application and add\_library in library)  
| – Library 3 (add\_subdirectory in application and add\_library in library)  
| – etc

There are few questions:

1. How to populate include directories correctly from for example Library 2 to Application?
2. How to populate include directories from Library 1 to Library3 and Application?

For now in Library CMakeLists.txt using this:

```auto
target_include_directories(${LIBRARY_1_TARGET}
	INTERFACE
		/dir1
		/dir2
		/dir3
)

```

And in Application using this:

```auto
target_include_directories(${APPLICATION_TARGET}
	PRIVATE
		$<TARGET_PROPERTY:Library_1,INTERFACE_INCLUDE_DIRECTORIES>
)

```

This approach allow me to populate include directories from library to Application. Is this approach correct from the way of view of CMake best practices. How to populate it from library to another library correctly? Is there way to setup library within automatic population through the all parts of application and application itself.

My global goal - to make library be attached within minimum lines of code in application. Kind of reusing approach.

---

<div class="post-metadata">

### Author: ![vito.gamberini](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vito.gamberini/32/4376_2.png) [@vito.gamberini](https://discourse.cmake.org/u/vito.gamberini)
#### Post date: [July 26, 2025, 1:43pm UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/2 "2025-07-26T13:43:09Z")

</div>

The modern mechanism is `FILE_SET HEADERS`

```cmake
target_sources(Library1
   INTERFACE
     FILE_SET interface_headers
     TYPE HEADERS
     BASE_DIRS
       dir1
       dir2
       dir3
     FILES
       dir1/file/to/be/installed.h
       dir2/file/to/be/installed.h
)

# Elsewhere
target_link_libraries(Application
  PRIVATE
    Library1
    Library2
    Library3
)

```

This is assuming you intend to `install()` the libraries at some point. If you don’t ever intend to install the libraries or headers, you don’t need to list any `FILES`. The file list itself is just to tell CMake which files need to be installed (and some IDEs use the metadata to list files in their explorer).

Interface links are transitive, ie:

```cmake
target_link_libraries(Library3
  INTERFACE
    Library2
)

target_link_libraries(Application
  PRIVATE
    Library1
    Library3
)
# Application inherits Library2 usage requirements from Library3

```

---

<div class="post-metadata">

### Author: ![alexandr](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@alexandr](https://discourse.cmake.org/u/alexandr)
#### Post date: [July 27, 2025, 5:33am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/3 "2025-07-27T05:33:54Z")

</div>

Just tried your way. Have no result at all.

Application CMakeLists.txt contain:

```auto
target_link_libraries(Application
	PRIVATE
		Library_1
		Library_2
)

```

Library1 and Library2 CMakeLists.txt contain:

```auto
target_sources(Library[1/2]
	INTERFACE
		FILE_SET interface_headers
		TYPE HEADERS
		BASE_DIRS
			/Dir1
			/Dir2
)

```

But still have this troubles:

```auto
fatal error: 'library_1.h' file not found

```

It’s starting working only for Application (still not available library in library) if adding to Application CMakeLists.txt this:

```auto
target_include_directories(Application
	PRIVATE
		$<TARGET_PROPERTY:Library_1,INTERFACE_INCLUDE_DIRECTORIES>
		$<TARGET_PROPERTY:Library_2,INTERFACE_INCLUDE_DIRECTORIES>
)

```

An it doesn’t matter using by me ‘target\_sources’ or ‘target\_include\_directories’ in Libraries CMakeLists.txt

---

<div class="post-metadata">

### Author: ![vito.gamberini](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vito.gamberini/32/4376_2.png) [@vito.gamberini](https://discourse.cmake.org/u/vito.gamberini)
#### Post date: [July 27, 2025, 5:37am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/4 "2025-07-27T05:37:42Z")

</div>

It’s not `/Dir1`, it’s `Dir1`. By putting the `/` before the directory you’ve made it into an absolute path, literally saying “`Dir1` in the root of the entire filesystem”.

You almost certainly want a relative path, no leading slash. Relative paths in most CMake contexts, including `target_sources()`, are relative to the current source directory

---

<div class="post-metadata">

### Author: ![alexandr](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@alexandr](https://discourse.cmake.org/u/alexandr)
#### Post date: [July 27, 2025, 5:40am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/5 "2025-07-27T05:40:14Z")

</div>

Dir1 or /Dir1 just for example written by me. For this exact situation Libraries using result of this:

```auto
${CMAKE_CURRENT_SOURCE_DIR}

```

For now writing examples within all-in-one directory.

---

<div class="post-metadata">

### Author: ![alexandr](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@alexandr](https://discourse.cmake.org/u/alexandr)
#### Post date: [July 27, 2025, 5:40am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/6 "2025-07-27T05:40:51Z")

</div>

This is piece of code:

```auto
set(${A_PREFIX}_DIR_ROOT ${CMAKE_CURRENT_SOURCE_DIR})

```

and after

```auto
target_sources(${A_NAME_TARGET}
	INTERFACE
		FILE_SET interface_headers
		TYPE HEADERS
		BASE_DIRS
			${${A_PREFIX}_DIR_ROOT}
)

```

or

```auto
target_include_directories(${A_NAME_TARGET}
	PUBLIC
		${${A_PREFIX}_DIR_ROOT}
)

```

---

<div class="post-metadata">

### Author: ![alexandr](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@alexandr](https://discourse.cmake.org/u/alexandr)
#### Post date: [July 27, 2025, 5:47am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/7 "2025-07-27T05:47:22Z")

</div>

Is there any requirements to write something before or after something?

---

<div class="post-metadata">

### Author: ![vito.gamberini](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vito.gamberini/32/4376_2.png) [@vito.gamberini](https://discourse.cmake.org/u/vito.gamberini)
#### Post date: [July 27, 2025, 6:02am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/8 "2025-07-27T06:02:17Z")

</div>

I’ve posted a complete demonstration here: [GitHub - nickelpro/discourse-fileset-example](https://github.com/nickelpro/discourse-fileset-example)

If you check the CI you’ll see this does indeed build and run on all platforms. If you’re having trouble with something specific in this example, or you have a usage that diverges from it, please post the complete reproducible example and I’ll demonstrate where the problem is.

---

<div class="post-metadata">

### Author: ![alexandr](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@alexandr](https://discourse.cmake.org/u/alexandr)
#### Post date: [July 27, 2025, 6:06am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/9 "2025-07-27T06:06:40Z")

</div>

Thx a lot.

Found few differences to my implementation. There are top-level CMakeLists.txt not for application. Application CMakeLists.txt is added to the project with ‘add\_subdirectory’. In my case Application is top-level CMakeLists.txt.

Will reorganize my project in accordance with your example and try everything you mentioned in your messages.

---

<div class="post-metadata">

### Author: ![alexandr](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@alexandr](https://discourse.cmake.org/u/alexandr)
#### Post date: [July 27, 2025, 6:07am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/10 "2025-07-27T06:07:20Z")

</div>

Will reply soon with result of my tests

---

<div class="post-metadata">

### Author: ![vito.gamberini](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vito.gamberini/32/4376_2.png) [@vito.gamberini](https://discourse.cmake.org/u/vito.gamberini)
#### Post date: [July 27, 2025, 6:07am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/11 "2025-07-27T06:07:37Z")

</div>

It shouldn’t matter in what directories you call these commands in so long as you get the relative paths correct. I chose completely arbitrarily to split things, assuming maybe that was what you were doing.

---

<div class="post-metadata">

### Author: ![vito.gamberini](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vito.gamberini/32/4376_2.png) [@vito.gamberini](https://discourse.cmake.org/u/vito.gamberini)
#### Post date: [July 27, 2025, 6:10am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/12 "2025-07-27T06:10:27Z")

</div>

In this branch I’ve moved the `application` target to the top level: [GitHub - nickelpro/discourse-fileset-example at app-in-top-level](https://github.com/nickelpro/discourse-fileset-example/tree/app-in-top-level)

---

<div class="post-metadata">

### Author: ![alexandr](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@alexandr](https://discourse.cmake.org/u/alexandr)
#### Post date: [July 27, 2025, 6:11am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/13 "2025-07-27T06:11:27Z")

</div>

Not directories. Top level project.  
Your schema looks like:

Root CMakeLists.txt  
|-- Application (add\_subdirectory in root and add\_executable in Application)  
|-- Library (add\_subdirectory in root and add\_library in Library)

In my situation:

Application CMakeLists.txt  
|-- Library (add\_subdirectory in Application and add\_library in Library)

---

<div class="post-metadata">

### Author: ![vito.gamberini](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vito.gamberini/32/4376_2.png) [@vito.gamberini](https://discourse.cmake.org/u/vito.gamberini)
#### Post date: [July 27, 2025, 6:12am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/14 "2025-07-27T06:12:26Z")

</div>

Yes, that’s what I’ve done in the branch, your situation. There is no `add_subdirectory` for the `application` anymore.

---

<div class="post-metadata">

### Author: ![alexandr](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@alexandr](https://discourse.cmake.org/u/alexandr)
#### Post date: [July 27, 2025, 6:14am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/15 "2025-07-27T06:14:45Z")

</div>

Will check it out and reply.  
Really don’t know what is going on here.  
4 days trying to find the cause of it.  
Been reading manuals and examples many times.

---

<div class="post-metadata">

### Author: ![alexandr](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@alexandr](https://discourse.cmake.org/u/alexandr)
#### Post date: [July 27, 2025, 6:24am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/16 "2025-07-27T06:24:55Z")

</div>

First difference

```auto
[cmake] -- Configuring incomplete, errors occurred!
[cmake] CMake Error at CMakeLists.txt:1 (cmake_minimum_required):
[cmake] CMake 3.31 or higher is required. You are running version 3.30.5

```

Is it critical to use your version?  
Just lowered version to 3.30

---

<div class="post-metadata">

### Author: ![alexandr](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@alexandr](https://discourse.cmake.org/u/alexandr)
#### Post date: [July 27, 2025, 6:56am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/17 "2025-07-27T06:56:36Z")

</div>

Found another one difference. My Library located outside of Application or Root folder. In ‘add\_subdirectory’ using absolute path. Could it be the reason of troubles?

---

<div class="post-metadata">

### Author: ![vito.gamberini](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vito.gamberini/32/4376_2.png) [@vito.gamberini](https://discourse.cmake.org/u/vito.gamberini)
#### Post date: [July 27, 2025, 7:13am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/18 "2025-07-27T07:13:52Z")

</div>

That’s not a subdirectory, it’s not even really part of the same project. All source requirements of a given project are required to be in a single source tree underneath a top level.

`add_subdirectory()` won’t work at all if the targeted folder is not a subdirectory of the top-level, you will get an error akin to:

```log
CMake Error at CMakeLists.txt:12 (add_subdirectory):
  add_subdirectory not given a binary directory but the given source
  directory "/.../discourse-fileset-example/lib1" is not a
  subdirectory of
  "/.../discourse-fileset-example/application". When
  specifying an out-of-tree source a binary directory must be explicitly
  specified.

```

I’ve added an example demonstrating this just so you can see the error message for yourself: [GitHub - nickelpro/discourse-fileset-example at app-with-different-roots](https://github.com/nickelpro/discourse-fileset-example/tree/app-with-different-roots)

If the library is not a part of the same source tree, you should build and install the library separately, then consume the library from said install tree in the application project.

Broadly the “source tree” is the thing you track in a single source control repository. The root of that repository is where your top-level CMakeLists.txt should go most of the time.

---

<div class="post-metadata">

### Author: ![alexandr](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/71c47a/32.png) [@alexandr](https://discourse.cmake.org/u/alexandr)
#### Post date: [July 31, 2025, 6:19am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/19 "2025-07-31T06:19:46Z")

</div>

Spent few days on tests and experiments. Got published own example based on Qt [arboreus\_examples/qt6/CMake/ProjectArchitecture/ProjectArchitecture\_v4 at master · ArboreusSystems/arboreus\_examples · GitHub](https://github.com/ArboreusSystems/arboreus_examples/tree/master/qt6/CMake/ProjectArchitecture/ProjectArchitecture_v4)

My global goal is to make correctly working when Libraries outside of Project tree. Have few libraries that is used by different projects. Want to make possible using one source located outside for using in different projects. For me it’s critical. Don’t want to multiply physically one sources to different projects.

Maybe my solution is kind of hack. But it’s working.

The solution:

– creating symlinks on libraries in project tree  
– adding libraries to project with ‘add\_subdirectory’ (it’s working for both approaches ‘application-in-root’ and ‘root-application’)

Only one not working automatically - adding includes of library to another library. It’s still require using something like this:

```auto
target_include_directories(Library1
	PRIVATE
		$<TARGET_PROPERTY:Library_2,INTERFACE_INCLUDE_DIRECTORIES>
		$<TARGET_PROPERTY:Library_3,INTERFACE_INCLUDE_DIRECTORIES>
)

```

For me it’s Ok if all is working and project is building correctly.

SUPER-MEGA-HUGE Thanks for assistance.

---

<div class="post-metadata">

### Author: ![vito.gamberini](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vito.gamberini/32/4376_2.png) [@vito.gamberini](https://discourse.cmake.org/u/vito.gamberini)
#### Post date: [July 31, 2025, 6:29am UTC](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449/20 "2025-07-31T06:29:48Z")

</div>

You’re very much in “whatever works, works” territory here.

> [@alexandr](#):
>
> My global goal is to make correctly working when Libraries outside of Project tree.

Consuming libraries outside the source tree via `add_subdirectory()` is explicitly not supported. Consuming source libraries (ie, not `IMPORTED`) from outside the source tree at all isn’t an orthodox workflow.

I would encourage you to learn how to use `target_sources(FILE_SET)`, and how to install libraries and consume them via `find_package()`, as it will significantly improve your projects’ robustness, and allow them to be built and packaged by others.

That said, I’m glad you found a solution which worked for you.

[Next page](https://discourse.cmake.org/t/populate-include-directories-from-sub-directory-to-all-of-parts-of-the-project-how/14449.md?page=2)
