[CMake] How to compile a file only when linking anyhow?

Mark Jonas toertel at gmail.com
Wed Apr 16 12:37:51 EDT 2008


Hi,

what I am trying to achieve is that every time my application is
linked a C file is compiled just before linking. Nothing should be
done if linking is not necessary.

The reason for wanting this is that I want to give the application a
time stamp when it was linked. The time stamp should be a C string. A
C file with this contents would do:

char __build_timestamp__[]=  __DATE__ " " __TIME__ ;

I tried the following but it does not do what I want. It generates and
compiles the file only once.

ADD_CUSTOM_COMMAND(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/build_timestamp.c
COMMAND echo
ARGS 'char __build_timestamp__[]=  __DATE__ \" \" __TIME__ \;' >
${CMAKE_CURRENT_BINARY_DIR}/build_timestamp.c
)
SET( SRCS ${SRCS}
${CMAKE_CURRENT_BINARY_DIR}/build_timestamp.c
)

ADD_EXECUTABLE(${EXENAME} ${SRCS})

How do I tell the custom command to run when the linker will be
called? It should not run always because that would cause the linker
to be called whenever I call make.

A dependency to ${SRCS} would not be enough because when only a .h
file changes the linker would run but the generated C file wouldn't be
recompiled.

BTW, I am using CMake 2.6.

TIA,
Mark


More information about the CMake mailing list