[CMake] Generating include files
Michael Ellery
mellery451 at gmail.com
Fri May 19 17:13:36 EDT 2017
> On May 19, 2017, at 2:00 PM, Urs Thuermann <urs at isnogud.escape.de> wrote:
>
> Michael Ellery <mellery451 at gmail.com> writes:
>
>> if your source can be generated by simple substitution of variable
>> values (like a template file), then configure_file() will do the
>> trick. If the process to create the file is more complex, then
>> add_custom_command() with an appropriate OUTPUT specification is
>> probably what you want, something like:
>
> Yes, the awk example was a simplification. The table to be generated
> is much larger using a somewhat more complex specification. I don't
> want to calculate at run-time in foo.c, but at compile-time so that it
> can go into a static const int array.
>
>> add_custom_command(OUTPUT tab.c COMMAND "awk -f mktab > tab.c")
>
> I have tried dozens of combinations with add_custom_command(),
> include_directories(), and ${CMAKE_BINARY_DIR}. Also the way you
> show. But the only way I found to have tab.c actually generated was
> to put tab.c into the add_executable() specification, but then it
> generates a tab.o which is linked into the executable. But I want to
> have the tab[] static, i.e. included into a C file, not linked in.
>
>> You will need to make sure that some other target explicitly depends
>> on tab.c (or whatever you name the output) to cause this custom rule
>> to be executed.
>
> Is there a way to specify that foo.o depends on tab.c? For some
> reason cmake's scan does not seem to find this.
>
well, if you rename tab.c to tab.h, I’m pretty sure CMAKE will honor it as a dependency but won’t compile it..so that might be the easiest.
Alternatively, use set_source_file_properties to set https://cmake.org/cmake/help/v3.8/prop_sf/OBJECT_DEPENDS.html#prop_sf:OBJECT_DEPENDS
-Mike
More information about the CMake
mailing list