# Establish dependency on header files generated by configure\_file

**URL:** https://discourse.cmake.org/t/establish-dependency-on-header-files-generated-by-configure-file/3878
**Category:** Usage
**Tags:** os:linux
**Created:** [August 5, 2021, 12:25am UTC](https://discourse.cmake.org/t/establish-dependency-on-header-files-generated-by-configure-file/3878 "2021-08-05T00:25:18Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![oscarh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/aca169/32.png) [@oscarh](https://discourse.cmake.org/u/oscarh)
#### Post date: [August 5, 2021, 12:25am UTC](https://discourse.cmake.org/t/establish-dependency-on-header-files-generated-by-configure-file/3878/1 "2021-08-05T00:25:18Z")

</div>

My script generates a configure .h file from a template file by calling command configure\_file:

```auto
configure_file(config.h.in efc_fw_config.h)

```

But my source files do not directly #include “efc\_fw\_config.h”. Instead, we used compiler option -include to include the generated file efc\_fw\_config.h. The ‘-include’ option includes the source code of the specified file at the beginning of the compilation as if it were included at the first line of the compilation unit. The problem is, it seems that CMake cannot figure out all of my source files depend on this generated file. When I make a change in the template config.h.in, then run “cmake --build .”, CMake is smart enough to regenerate efc\_fw\_config.h, but it does not recompile my source files at all.

Is there any elegant way to tell CMake that the generated file efc\_fw\_config.h is a dependency of all source files?

---

<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: [August 5, 2021, 9:54am UTC](https://discourse.cmake.org/t/establish-dependency-on-header-files-generated-by-configure-file/3878/2 "2021-08-05T09:54:08Z")

</div>

Have a look at source property [OBJECT\_DEPENDS](https://cmake.org/cmake/help/latest/prop_sf/OBJECT_DEPENDS.html).

---

<div class="post-metadata">

### Author: ![oscarh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/aca169/32.png) [@oscarh](https://discourse.cmake.org/u/oscarh)
#### Post date: [August 5, 2021, 11:15am UTC](https://discourse.cmake.org/t/establish-dependency-on-header-files-generated-by-configure-file/3878/3 "2021-08-05T11:15:28Z")

</div>

Do I have to set OBJECT\_DEPENDS property for each source files?

---

<div class="post-metadata">

### Author: ![oscarh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/aca169/32.png) [@oscarh](https://discourse.cmake.org/u/oscarh)
#### Post date: [August 5, 2021, 11:44am UTC](https://discourse.cmake.org/t/establish-dependency-on-header-files-generated-by-configure-file/3878/4 "2021-08-05T11:44:48Z")

</div>

@marc.chevrier, I tend to think this is a CMake bug. Given -include option, “armclang -M” can generate a correct dependency list.  
For example, here is my simple main.c:

```auto
#include "mylib.h"

int add(int a, int b)
{
    return a+b;
}

int main()
{
    return add(NUM_OF_A, NUM_OF_B);
}

```

With this command:

```auto
armclang --target=arm-arm-none-eabi -march=armv8-a -include config.h -M main.c >deps.d

```

The content of deps.d:  
_main.o: main.c config.h mylib.h_

Does CMake take into compiler options when generating dependency list?

---

<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: [August 5, 2021, 1:47pm UTC](https://discourse.cmake.org/t/establish-dependency-on-header-files-generated-by-configure-file/3878/5 "2021-08-05T13:47:33Z")

</div>

`OBJECT_DEPENDS` is a **source** property, so… Yes.

---

<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: [August 5, 2021, 1:49pm UTC](https://discourse.cmake.org/t/establish-dependency-on-header-files-generated-by-configure-file/3878/6 "2021-08-05T13:49:15Z")

</div>

Dependencies are computed differently depending on `CMake` version as well as generator.

What are your `CMake` version and generator?

---

<div class="post-metadata">

### Author: ![oscarh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/aca169/32.png) [@oscarh](https://discourse.cmake.org/u/oscarh)
#### Post date: [August 5, 2021, 2:14pm UTC](https://discourse.cmake.org/t/establish-dependency-on-header-files-generated-by-configure-file/3878/7 "2021-08-05T14:14:38Z")

</div>

I’m using CMake 3.20.0. And the generator is Unix Makefile.

---

<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: [August 5, 2021, 2:28pm UTC](https://discourse.cmake.org/t/establish-dependency-on-header-files-generated-by-configure-file/3878/8 "2021-08-05T14:28:10Z")

</div>

In this environment, `CMake` is able to use the compiler to generate dependencies.

But I am not sure if this feature is activated for this specific compiler. Can you check, in your top-level `CMakeLists.txt`, what are the values of the following variables:

- CMAKE\_DEPFILE\_FLAGS\_C
- CMAKE\_C\_DEPENDS\_USE\_COMPILER

---

<div class="post-metadata">

### Author: ![oscarh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/aca169/32.png) [@oscarh](https://discourse.cmake.org/u/oscarh)
#### Post date: [August 5, 2021, 2:38pm UTC](https://discourse.cmake.org/t/establish-dependency-on-header-files-generated-by-configure-file/3878/9 "2021-08-05T14:38:58Z")

</div>

CMAKE\_DEPFILE\_FLAGS\_C is -MD -MT \<DEP\_TARGET\> -MF \<DEP\_FILE\>  
CMAKE\_C\_DEPENDS\_USE\_COMPILER is not defined.

---

<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: [August 5, 2021, 2:51pm UTC](https://discourse.cmake.org/t/establish-dependency-on-header-files-generated-by-configure-file/3878/10 "2021-08-05T14:51:13Z")

</div>

So, as I suspected, compiler generated dependencies are not activated for this compiler.

Can you add the following code in macro `__compiler_armclang` (file `Modules/Compiler/ARMCLang.cmake`). For example, at the end of the macro:

```cmake
if((NOT DEFINED CMAKE_DEPENDS_USE_COMPILER OR CMAKE_DEPENDS_USE_COMPILER)
    AND CMAKE_GENERATOR MATCHES "Makefiles|WMake"
    AND CMAKE_DEPFILE_FLAGS_${lang})
  # dependencies are computed by the compiler itself
  set(CMAKE_${lang}_DEPFILE_FORMAT gcc)
  set(CMAKE_${lang}_DEPENDS_USE_COMPILER TRUE)
endif()

```

And relaunch a generation from scratch. If all is OK, during compilation, flags defined in `CMAKE_DEPFILE_FLAGS_C` variable should be part of the compilation. And, of course, your dependency should be taken into account.

---

<div class="post-metadata">

### Author: ![oscarh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/aca169/32.png) [@oscarh](https://discourse.cmake.org/u/oscarh)
#### Post date: [August 5, 2021, 10:53pm UTC](https://discourse.cmake.org/t/establish-dependency-on-header-files-generated-by-configure-file/3878/11 "2021-08-05T22:53:57Z")

</div>

@marc.chevrier , with your suggested code change, the “-include” option is taken into account when the dependency list is generated. The behavior is as expected. Would you plan to include this fix in next release?

An interesting observation: if I simply “touch config.h.in” followed by “cmake --build”, the build system is reconfigured, but no recompilation. Recompilation happens only after I make a real content change in config.h.in. Does CMake watch the content change in addition to the file timestamp?

Thank you, Marc.

---

<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: [August 6, 2021, 9:37am UTC](https://discourse.cmake.org/t/establish-dependency-on-header-files-generated-by-configure-file/3878/12 "2021-08-06T09:37:45Z")

</div>

`configure_file` regenerate the if, and only if, contents has changed.

I will evaluate the possibility to integrate this evolution in next release: see [MR](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/6433).
