Take the 2-minute tour ×

I have a configuration file setup that defines the correct header files to include for certain components. Later I include that file via the preprocessor token. Unfortunately CMake's dependency scanner completely ignores the header file.

Essentially it comes down to this:

#define HEADER_FILE "somefile.h"
#include HEADER_FILE

CMake does not add "somefile.h" to the list of dependencies for this source file!

How can I get CMake to recognize this dependency correctly?

(I know that I can do #if 0 blocks and include all the files, but this either includes too many dependencides for other sources, or defeats the whole purpose in the first place -- depending on how you do it)

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

You can add an explicit dependency to a source file by setting the OBJECT_DEPENDS property:

set_property(SOURCE source.cpp APPEND PROPERTY OBJECT_DEPENDS "somefile.h")

You'll have to do this for any source file that includes your configuration file.

share|improve this answer
Thanks, this actually helps me in another situation as well (dealing with unsupported source files). –  edA-qa mort-ora-y Jul 1 '11 at 12:58
add comment

May I suggest to make the header file generic instead of all the sourcefiles? This is also more common than your approach that I never saw in any production code.

Like this:

// meh.hh
#ifndef MEH_HH
#define MEH_HH

#ifdef THIS
# include <this>
#elif defined(THAT)
# include <that>
# error meh

#endif // MEH_HH

// main.cc
#include "meh.hh"

int main () {...}
share|improve this answer
This is exactly what I am trying to get away from (it's what I did before). Rather than simply have a define for each header I'd instead have to have a configurable header file for all those headers. –  edA-qa mort-ora-y Jul 1 '11 at 9:37
Why would your approach be superior to the canonical one? Not only CMake will struggle with it, but also IDEs and editors (think of "switch header/source" or "goto file") –  phresnel Jul 1 '11 at 10:08
My approach is the one supported by the C preprocessor: Simply set a DEFINE to the name of the file to include. It doesn't involve writing wrapper files, thus less to maintain. –  edA-qa mort-ora-y Jul 1 '11 at 10:48
Mine is certainly supported by the C preprocessor as well ;) Documentation is also maintenance, and I think a handful of wrapper files is better at self-documentation because they enlist available definitions, and it could even made to work by adding the #defines at the top of those wrappers. Your approach means you either need to include the defining header + the header, or to supply the definition directly to the compiler. –  phresnel Jul 1 '11 at 11:10
add comment

Your Answer


