开发者

CMake dependency on defined (preprocessor) header file

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)


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.


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>
#else
# error meh
#endif 

#endif // MEH_HH

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

int main () {...}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜