So I'm trying to convert a make-based project to cmake, and having trouble wrapping my head around how it works. I've figured out how to get custom commands to generate a header file, and how to compile source file for the target executable, but I can't seem to link them together -- I can't figure out how to trigger the custom command to generate the header file. Here's a trivial example of what I'm trying to do:
CMakeLists.txt:
add_executable(test test.c)
add_custom_command(OUTPUT foo.h
COMMAND echo "/* test */" > foo.h
)
test.c:
#include "foo.h"
int main() { return 0; }
However, when I run cmake and make, it gives me:
/home/cdodd/test/test.c:1:17: fatal error: foo.h: No such file or directory
compilation terminated.
It can't seem to figure out that it needs to create foo.h first before compiling test.c. With make I'd just add a dependency; how do I do that with cmake?