Using Dynamically Generated Header Files with CMake

  /     /   Programming     Tutorial    

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.

Related Articles

Adding a Basic Stub to a Vic-20 Assembly Language Program

To make machine language programs more friendly it is nice to add a basic stub which contains a line with a SYS statement to start the code. This is easy to do on the Vic-20 and the process gives you ...   Read More

Beginning Assembly Programming on the Commodore Vic-20

The Commodore Vic-20 is a great machine to learn an assembly language on. It was released in 1981 and was the first computer to sell one million units, which contributes to its popularity today. The ...   Read More

Compiling a Tcl Script into an Executable

Locating Tcl scripts to load from an executable can be awkward if you want to make your program cross-platform. An easier way is to compile a Tcl script directly into the executable and let that scrip...   Read More

Embedding an SDL Surface in a Tk Window

Tk is great, but sometimes it just isn't fast enough. SDL is fast, but has no support for input dialogs and other GUI conventions. By embedding an SDL surface in a Tk window you get the best of both ...   Read More

Running 4K FORTRAN on a DEC PDP-8

4K FORTRAN was a subset of FORTRAN II and was the first high-level language available for the DEC PDP-8. It consisted of two parts: the 4K FORTRAN Compiler (nicknamed "Fivetran") and the 4K ...   Read More

Sign up to get new articles straight to your inbox.

Delivered by FeedBurner

Comments

blog comments powered by Disqus