Using Dynamically Generated Header Files with CMake

Sometimes it can be useful to dynamically generate header files and include them from a C source file. However, it can be a little difficult getting CMake to recognize when to generate the files and when to recompile the source files that include those headers. What follows is a method to do this.

Telling CMake to Generate the Header File

To generate the header file we need to create a custom_command which will run the command to generate the header file, when the file it is based on has changed. The following shows how to use the hypothetical generatewordlistheader command to create wordlist.h everytime wordlist.txt changes.

add_custom_command(
    PRE_BUILD
    OUTPUT wordlist.h
    COMMAND generatewordlistheader wordlist.txt wordlist.h
    DEPENDS wordlist.txt
)

Telling CMake When to Recompile

Next we need to tell CMake to recompile any source files which include the generated header file, when the header file has changed. To do this we create a statically linked library, here called poet, that relies on the source files that include the header and the header itself.

add_library(poet STATIC poet.c ${MyApp_SOURCE_DIR}/src/wordlist.h)

Finally we need to ensure that the statically generated library above is linked into the application.

target_link_libraries(myapp poet)

Full example

Using a fictional project called MyApp we can see how this is put together.

cmake_minimum_required(VERSION 2.6)
project(MyApp)
add_executable(myapp myapp.c)

add_custom_command(
    PRE_BUILD
    OUTPUT wordlist.h
    COMMAND generatewordlistheader wordlist.txt wordlist.h
    DEPENDS wordlist.txt
)

add_library(poet STATIC poet.c ${MyApp_SOURCE_DIR}/src/wordlist.h)

target_link_libraries(myapp poet)

Conclusion

This isn't perfect but it does work. Unfortunately CMake is an ugly, but very useful tool. I'm sure there is a better way to do this and if anyone can think of a way, please leave a comment or email me. Until then, this achieves what I and a number of other people have been trying to do.

Creative Commons License
Using Dynamically Generated Header Files with CMake by Lawrence Woodman is licensed under a Creative Commons Attribution 4.0 International License.

Share This Post

Feedback/Discuss

Related Articles

Modula-2 Compilers on CP/M

Modula-2 is a great language in general and is a good choice for programming on CP/M. There are three good compilers available for CP/M which all require a Z80 processor and we'll compare each in turn...   Read More

Is SUBLEQ the Right Choice for a VM?

SUBLEQ is an interesting architecture because of its simplicity, adaptability and power. It is therefore an attractive choice for a simple virtual machine. However, this comes at a cost which we will...   Read More

80 Columns in Software on the Commodore VIC-20

If you have good eyesight, a well-tuned display and patience it is possible to use 80 columns in software on the VIC-20. This is really just an experiment but considering the limitations of the Vic I ...   Read More

64 Column Text Mode on the Commodore VIC-20

With a little lateral thinking and by putting the television on its side we can create a 64 column sideways text mode on the VIC-20. This article will demonstrate this and show how it is done. Previo...   Read More

Getting the Address of BASIC Variables on the VIC-20

Getting the address of a BASIC variable can be useful if you want to pass data to a machine code routine or want to access the bytes of a variable directly to improve speed and reduce garbage collectio...   Read More