# C++ Header Files and Dependency Management in CMake

**URL:** <https://discourse.cmake.org/t/c-header-files-and-dependency-management-in-cmake/10909>\
**Category:** Code\
**Created:** [May 26, 2024, 3:00pm UTC](https://discourse.cmake.org/t/c-header-files-and-dependency-management-in-cmake/10909 "2024-05-26T15:00:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mredenti](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mredenti/32/4464_2.png) [@mredenti](https://discourse.cmake.org/u/mredenti)\
**Post date:** [May 26, 2024, 3:00pm UTC](https://discourse.cmake.org/t/c-header-files-and-dependency-management-in-cmake/10909/1 "2024-05-26T15:00:25Z")

</div>

When creating targets in CMake using the `add_executable` or `add_library` commands, do I need to explicitly specify header files in order to create the dependency tree for automatic build triggering, or does CMake handle this automatically?

For example, consider the following CMake configuration:

```auto
cmake_minimum_required(VERSION 3.10)
project(ExampleProject)

add_executable(ExampleApp main.cpp utils.cpp)
# Should I list the headers here too?
# target_sources(ExampleApp PRIVATE main.cpp utils.cpp utils.h)

```

In this example, `main.cpp` includes `utils.h`. If I modify `utils.h`, will CMake automatically detect this change and trigger a rebuild of `ExampleApp`, or do I need to specify `utils.h` in the `add_executable` command or another command to ensure the dependency is tracked?

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [May 26, 2024, 3:22pm UTC](https://discourse.cmake.org/t/c-header-files-and-dependency-management-in-cmake/10909/2 "2024-05-26T15:22:22Z")

</div>

CMake manage automatically such dependencies. Moreover, for the most common compilers, CMake relies on the compiler itself to discover these dependencies.

---

<div class="post-metadata">

**Author:** ![mredenti](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mredenti/32/4464_2.png) [@mredenti](https://discourse.cmake.org/u/mredenti)\
**Post date:** [May 26, 2024, 4:02pm UTC](https://discourse.cmake.org/t/c-header-files-and-dependency-management-in-cmake/10909/3 "2024-05-26T16:02:13Z")

</div>

Yeah, I see that gnu compilers support dependency discovery (-MD ) whereas with Fortran CMake does the scanning of dependencies. So the header files are completely ignored? or are they useful for IDEs?

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [May 26, 2024, 4:07pm UTC](https://discourse.cmake.org/t/c-header-files-and-dependency-management-in-cmake/10909/4 "2024-05-26T16:07:42Z")

</div>

Header files, as specified as part of `add_library()` or `add_executable()` commands, are not compilable files so are ignored for the compilation step but, as you suggest, will be shown in the IDEs.

---

<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:** [March 13, 2025, 6:23pm UTC](https://discourse.cmake.org/t/c-header-files-and-dependency-management-in-cmake/10909/5 "2025-03-13T18:23:16Z")

</div>

Note that with `target_sources(FILE_SET)`, listing headers can plumb them not only into being listed in IDEs, but also part of installing the target and automatically populating the usage include directories as needed for the build, build export, and install export automatically.
