[CMake] How to configure target or command to preprocess C file?

Mateusz Loskot mateusz at loskot.net
Sun Jul 8 08:34:15 EDT 2012


Hi,

I'm porting build configuration based on GNU Autotools to CMake
and I have to deal with C preprocessing to generate a file.

The input for preprocessor is SQL file with C preprocessor directives
used, like #include "another.sql", etc.

Currently, Makefile uses the following rule to generate plain SQL file
as output:

myfile.sql: myfile.sql.in.c
    cpp -I../common $< | grep -v '^#' > $@

So, the myfile.sql is meant to be one of products of the build process,
similar to share libraries or executables.

What CMake tools should I use to achieve the same effect?

It's unclear to me if I should use add_custom_command,
add_custom_target or combine both.

Obviously, I'm looking for a portable solution that would work at least
with GNU GCC and Visual Studio toolsets. I presume I will have to define
platform-specific custom commands, one for cpp preprocessor, one for cl.exe.
Or, does CMake provide some kind of abstraction for C-preprocessing?

I scanned the archives, but I only found preprocessing of fortran files
or solutions based on make capabilities (make myfile.i).
So, it's not quite what I'm looking for.

Could anyone help me with this?

Best regards,
-- 
Mateusz Loskot, http://mateusz.loskot.net


More information about the CMake mailing list