legends2k
legends2k

Reputation: 32984

Dirty generated header doesn't rebuild dependant target

I've a target which has some headers in its SOURCES among others; these are generated using add_custom_command. Here's the excerpt

set(MY_SOURCES
  a.txt
  b.txt)

set(MY_HEADERS)
foreach (TXT ${MY_SOURCES})
  add_custom_command(OUTPUT ${TXT}.h
    COMMAND txt2h
    -i "${CMAKE_CURRENT_SOURCE_DIR}/${TXT}"
    -o "${TXT}.h"
    MAIN_DEPENDENCY ${TXT}
    COMMENT "Generating header from ${TXT}"
    VERBATIM)
  list(APPEND MY_HEADERS "${CMAKE_CURRENT_BINARY_DIR}/${TXT}.h")
endforeach ()

add_executable(exe main.cpp ${MY_HEADERS})
target_include_directories(exe PRIVATE ${CMAKE_CURRENT_BINARY_DIR})

This generates (cmake -B build) and builds (cmake --build build) fine.

When I edit a.txt and run cmake --build build. It regenerates a.txt.h since I see

Generating header from a.txt

However, the exe target isn't built. Since the generated header is listed as a source for exe, I'm hoping it should also make exe dirty and rebuild it. What am I missing?

I verified the timestamps on a.txt and a.txt.h match and are newer than the exe's.

References

  1. Adding a custom command and generated file (Official CMake tutorial)
  2. Generated sources in CMake builds

Upvotes: 0

Views: 298

Answers (1)

KamilCuk
KamilCuk

Reputation: 141698

Header files are not compiled. Executable depends on object files, which are produced from cpp files. Only when one of cpp files include the header, then it needs to be recompiled when the header changes, then the executable will be recompiled when the header changes.

Adding the header to add_executable only makes it show in some IDEs, it has no effect.

Upvotes: 2

Related Questions