I'm trying to generate a header with a custom command. The header should be updated on each rebuild, so that the source file which includes it would also be rebuilt. (Actual command is a script, but here is a simplified version.)
Here's my project:
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
set(test_SOURCES test.c)
include_directories("${CMAKE_BINARY_DIR}")
set(VERSION_H_PATH "${CMAKE_BINARY_DIR}/version.h")
message("VERSION_H_PATH: ${VERSION_H_PATH}")
add_custom_command(OUTPUT "${VERSION_H_PATH}" COMMAND "touch" "${VERSION_H_PATH}")
#add_custom_target(GENERATE COMMAND "touch" "${VERSION_H_PATH}")
add_executable(myprog ${test_SOURCES})
add_dependencies(myprog GENERATE)
test.c
#include <version.h>
int main()
{
return 0;
}
Now the problem is that the CMakeList.txt
, as presented above, doesn't result in version.h
being created at all. Only after I switch from add_custom_target
to add_custom_command
does it do. But then, if I change the file somehow, next make
doesn't rebuild the project.
Looks like CMake doesn't recognize that test.c
depends on version.h
, although it does explicitly #include
it. What am I doing wrong here?