[CMake] file not generated with ADD_CUSTOM_COMMAND

William A. Hoffman billlist at nycap.rr.com
Wed Jan 4 13:13:44 EST 2006


At 12:38 PM 1/4/2006, you wrote:
>Hi, 
> 
>shouldn't this work ? 
> 
>main.c: 
> 
>#include "generated.h" 
> 
>#include <stdio.h> 
> 
>int main() 
>{ 
>   printf("hello world\n"); 
>   return 0; 
>} 
> 
>CMakeLists.txt: 
> 
>ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated.h COMMAND 
>sh ARGS -c "echo // > generated.h" ) 
>ADD_EXECUTABLE(hello main.cpp)


See the FAQ:
http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_generate_a_source_file_during_the_build.3F



Generating a header file works similarly. Note that generated headers can often cause unnecessary rebuilds and should be avoided if possible. Consider using the CONFIGURE_FILE command to prepare the header at CMake time. If you must generate a header file, use code like this: 


 ADD_CUSTOM_COMMAND(
   OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/foo.h
   COMMAND ${CMAKE_COMMAND}
   ARGS -E copy ${CMAKE_CURRENT_SOURCE_DIR}/bar.h
                ${CMAKE_CURRENT_BINARY_DIR}/foo.h
   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/bar.h
   )
 SET_SOURCE_FILES_PROPERTIES(foo.c PROPERTIES
   OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/foo.h)
 ADD_EXECUTABLE(foo foo.c)


The generation of the header works similarly to the first example above. Since the header does not exist at the time foo.c's dependencies are scanned there is no way for CMake to know that it requires foo.h. The OBJECT_DEPENDS source file property gives CMake this knowledge. This custom command will automatically be placed in the generated build system for any target using a source file with this OBJECT_DEPENDS property. 



More information about the CMake mailing list