I have a header file version.h
containing version information. Before every build, I want CMake to ensure that the version information is up to date. As a test, I've set up CMake to touch the file before every build. That should cause all source files that include the file to be recompiled on every build. Here is how it works:
# CMakeLists.txt
add_library(MY_APP STATIC main.cpp version.h)
add_custom_target(UPDATE_VERSION
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_SOURCE_DIR}/version.h
)
add_dependencies(MY_APP UPDATE_VERSION)
// main.cpp
#include "version.h"
On every build, the first output I get is /usr/local/bin/cmake -E touch version.h
, and the timestamp of the file is changed. Great! However, main.cpp
is only compiled on every other build! It seems that whether main.cpp
should be compiled is determined before version.h
is updated. Probably something like this happens:
- List
main.cpp
for compilation ifversion.h
is newer thanmain.obj
touch version.h
- Compile
main.cpp
if it was listed for compilation
Whenever main.cpp
is compiled, main.obj
will be newer than version.h
in step 1 during the next build, so then main.cpp
will not be compiled. In the next run again, version.h
will be newer than main.obj
in step 1, and main.cpp
is compiled again.
At least this is my assumption to why it does not work. How can I update build version.h
so that main.cpp
is compiled every time?
I'm using Visual Studio 2022 and Ninja.