[CMake] Bug? add_custom_command() ignores head file outputs

Andreas Pakulat apaku at gmx.de
Fri Aug 24 14:09:56 EDT 2012


Hi,

On Fri, Aug 24, 2012 at 7:27 PM, Adam B <adamb at videx.com> wrote:
> Hello all,
>
> I'm relatively new to CMake but I've encountered some unexpected behavior.
> Perhaps it's by design or perhaps it's a bug - you tell me.  The short story
> is this:

It is a bug, but not in CMake, its in your cmake code.

> It appears that add_custom_command() is ignored if the only OUTPUT is a C
> header file (.h).  The generated makefiles do not include the custom
> command.  However, if I also list a C source file (.c) as a second output,
> the makefiles get the command.

If you look at the add_custom_command documentation you'll notice that
it says "if a target defined in the same directory depends on the
output of a custom command a dependency rule is set up so the custom
command runs before the target is being built" (slightly different
wording, written from the top of my head). Your target however only
has the .c file in its source list and hence nothing in your target
depends on an output of the custom command and hence no such rule is
set up. You can either include the .h file in the list of sources for
your target (CMake does not generate compile-rules for files it
recognizes as C/C++ headers), you can setup a custom target that
depends on the .h file and has some no-op as command and make your
executable depend on the custom target or (as you already did) have
the .c file in the output of the custom command.

Andreas


More information about the CMake mailing list