# How do I get generated files to be built first?

**URL:** https://discourse.cmake.org/t/how-do-i-get-generated-files-to-be-built-first/13370
**Category:** Usage
**Created:** [January 10, 2025, 6:10pm UTC](https://discourse.cmake.org/t/how-do-i-get-generated-files-to-be-built-first/13370 "2025-01-10T18:10:52Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![rsalz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/rsalz/32/5179_2.png) [@rsalz](https://discourse.cmake.org/u/rsalz)
#### Post date: [January 10, 2025, 6:10pm UTC](https://discourse.cmake.org/t/how-do-i-get-generated-files-to-be-built-first/13370/1 "2025-01-10T18:10:52Z")

</div>

I have a series of files include/openssl/foo.gen that have a custom command to build {build-dir}include/openssl/foo.h. And foo.h is include’d in various source files. How do I ensure that the step runs before compiling bar.c ? I thought it would come for free with dependency building, but that doesn’t seem to be done. Thanks.

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [January 11, 2025, 12:17am UTC](https://discourse.cmake.org/t/how-do-i-get-generated-files-to-be-built-first/13370/2 "2025-01-11T00:17:42Z")

</div>

Add the generated headers with target\_sources()  
If possible use [FILE\_SET HEADER](https://cmake.org/cmake/help/latest/command/target_sources.html)

---

<div class="post-metadata">

### Author: ![rsalz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/rsalz/32/5179_2.png) [@rsalz](https://discourse.cmake.org/u/rsalz)
#### Post date: [January 11, 2025, 3:16pm UTC](https://discourse.cmake.org/t/how-do-i-get-generated-files-to-be-built-first/13370/3 "2025-01-11T15:16:25Z")

</div>

Thanks, but what path do I use?  
In the top-level CMakeLists I have  
foreach (f ${GENHEADERS}) # openssl/crypto.h, etc …  
set(in “include/${f}.in”)  
set(out “${CMAKE\_BINARY\_DIR}/include/${f}”)  
add\_custom\_command(COMMENT “Generating ${out}”  
OUTPUT “${out}” DEPENDS “${in}” ${mkhdr}  
COMMAND ${PERL} ${mkhdr} ${in} ${out}  
VERBATIM)  
endforeach ()

In ssl/CMakeLists I have  
add\_executable(openssl)  
target\_sources(openssl PUBLIC ${APPSOURCE} ${CMAKE\_BINARY\_DIR}/include/openssl/crypto.h)  
target\_link\_libraries(openssl crypto ssl)

I get the same error:  
; mk openssl  
[0%] Building C object ssl/CMakeFiles/ssl.dir/d1\_lib.c.o  
In file included from /home/rsalz/git/quictls/quictls/ssl/d1\_lib.c:10:  
/home/rsalz/git/quictls/quictls/include/internal/e\_os.h:17:11: fatal error: openssl/crypto.h: No such file or directory  
17 | # include \<openssl/crypto.h\>

> ^ ~~~~~~~~~~~~~~~~~

It’s not picking up the generated file as a dependency nor building it.

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [January 11, 2025, 5:09pm UTC](https://discourse.cmake.org/t/how-do-i-get-generated-files-to-be-built-first/13370/4 "2025-01-11T17:09:48Z")

</div>

> [@rsalz](#):
>
> add\_executable(openssl)  
> target\_sources(openssl PUBLIC ${APPSOURCE} ${CMAKE\_BINARY\_DIR}/include/openssl/crypto.h)  
> target\_link\_libraries(openssl crypto ssl)

As I recommended, use FILE\_SET HEADER, or set the needed include path yourself:

```cmake
add_library(crypto)
target_sources(crypto PRIVATE ${CRYPTO_SOURCES} 
    PUBLIC
    FILE_SET public_headers
    TYPE HEADERS
    BASE_DIRS
         ${CMAKE_BINARY_DIR}/include # ....
    FILES
         ${CMAKE_BINARY_DIR}/include/openssl/crypto.h # ...
)

# later 
# ...
target_link_libraries(openssl PUBLIC crypto ssl)

```

---

<div class="post-metadata">

### Author: ![rsalz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/rsalz/32/5179_2.png) [@rsalz](https://discourse.cmake.org/u/rsalz)
#### Post date: [January 11, 2025, 7:23pm UTC](https://discourse.cmake.org/t/how-do-i-get-generated-files-to-be-built-first/13370/5 "2025-01-11T19:23:45Z")

</div>

I missed the FILE\_SET. Thanks for your patience!

---

<div class="post-metadata">

### Author: ![rsalz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/rsalz/32/5179_2.png) [@rsalz](https://discourse.cmake.org/u/rsalz)
#### Post date: [January 11, 2025, 8:50pm UTC](https://discourse.cmake.org/t/how-do-i-get-generated-files-to-be-built-first/13370/6 "2025-01-11T20:50:40Z")

</div>

And it turns out the email I got sent cut off that key line! I’m good to go now, thanks again.

---

<div class="post-metadata">

### Author: ![rsalz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/rsalz/32/5179_2.png) [@rsalz](https://discourse.cmake.org/u/rsalz)
#### Post date: [January 14, 2025, 6:39pm UTC](https://discourse.cmake.org/t/how-do-i-get-generated-files-to-be-built-first/13370/7 "2025-01-14T18:39:58Z")

</div>

A further complication was that “normal” header file foo.h did a `#include` of generated file crypto.h, so I had to use `add_custom_target` that depended on all the generated files, and then use `add_dependencies` on the each target to generate the files. (I’m using 3.21, so couldn’t use FILE\_SET)

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [January 15, 2025, 6:19am UTC](https://discourse.cmake.org/t/how-do-i-get-generated-files-to-be-built-first/13370/8 "2025-01-15T06:19:58Z")

</div>

> [@rsalz](#):
>
> (I’m using 3.21, so couldn’t use FILE\_SET)

There is no real reason to stay behind the future.

You may always install the newest version with:

```bash
pip install cmake

```

---

<div class="post-metadata">

### Author: ![rsalz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/rsalz/32/5179_2.png) [@rsalz](https://discourse.cmake.org/u/rsalz)
#### Post date: [January 15, 2025, 11:36pm UTC](https://discourse.cmake.org/t/how-do-i-get-generated-files-to-be-built-first/13370/9 "2025-01-15T23:36:57Z")

</div>

I’m using 3.21 because it’s what came on my Ubuntu VM and I since I’m aiming for enterprise users, “step 1: update cmake” isn’t appropriate IMO.

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [January 16, 2025, 10:15am UTC](https://discourse.cmake.org/t/how-do-i-get-generated-files-to-be-built-first/13370/10 "2025-01-16T10:15:23Z")

</div>

[Ubuntu – Informationen über Paket cmake in noble](https://packages.ubuntu.com/noble/cmake)

---

<div class="post-metadata">

### Author: ![rsalz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/rsalz/32/5179_2.png) [@rsalz](https://discourse.cmake.org/u/rsalz)
#### Post date: [January 16, 2025, 2:07pm UTC](https://discourse.cmake.org/t/how-do-i-get-generated-files-to-be-built-first/13370/11 "2025-01-16T14:07:25Z")

</div>

Thanks for the info. We’re currently on 22.04 LTS, not yet the next LTS release which seems to be in development.
