[CMake] custom command to create file #included in other file?

Markus Grabner grabner at icg.tugraz.at
Tue Jul 29 11:22:34 EDT 2008


	Hi!

    How can I write a custom command to create a source file which doesn't 
appear in the source file list of any target, but is included in another 
source file? Consider a file "file1.c", from which "file2.c" should be 
generated automatically whenever "file1.c" has been modified. However, 
both "file1.c" and "file2.c" are just code snippets which can't be compiled 
into separate object files, but must instead be included in "file3.c" like 
this:

void func1() {
#include "file1.c"
}

void func2() {
#include "file2.c"
}

So only "file3.c" contains syntactically valid C code (after preprocessing, 
i.e., including "file1.c" and "file2.c"), while "file1.c" and "file2.c" by 
themselves don't. Now the corresponding target would be

add_executable(file3 file3.c)

According to [1] and [2], the file to be generated should appear in the source 
list of a target. However, this is not possible here since it would result in 
a compile error. The "add_dependencies" command [3] also doesn't help since 
it can only be used for top level targets, but not for the output 
of "add_custom_command".

Since writing such rules in plain Makefile syntax by hand is trivial, I 
believe that cmake should also support this situation, but I just can't 
figure out how. Are there any ideas how to solve this?

	Thanks & kind regards,
		Markus


[1]
http://www.cmake.org/HTML/cmake-2.6.html#command:add_custom_command

[2]
http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_generate_a_source_file_during_the_build.3F

[3]
http://www.cmake.org/HTML/cmake-2.6.html#command:add_dependencies

-- 
Markus Grabner - Computer Graphics and Vision
Graz University of Technology, Inffeldgasse 16a/II, 8010 Graz, Austria
Phone: +43/316/873-5041, Fax: +43/316/873-5050
WWW: http://www.icg.tugraz.at/Members/grabner


More information about the CMake mailing list