Prevent header from being included in some files, in compilation time?

醉酒当歌 提交于 2019-12-14 04:17:55

问题


I have a header file, which I can control its contents.

Additionally I have an interface I1 (defined in some other file) from which various implementations derived. I want to prohibit those implementations from including this header file. So that during compile time if the file is included the compilation will fail, otherwise it will continue as usual.

So I have header file and an interface definition (in some other file). I want to prohibit interface implementations from including the given header file, during compilation.

Any suggestions as to how I can achieve that? Is there some clever templates/preprocessing trick I can use?


回答1:


In the header file:

#ifndef FOO_IMPLEMENTATION_USE_ONLY
#error This file is for inclusion in the FOO implementation only
#endif

In the files that are supposed to include it:

// Define this ONLY in the Foo implementation files
#define FOO_IMPLEMENTATION_USE_ONLY


来源:https://stackoverflow.com/questions/18824271/prevent-header-from-being-included-in-some-files-in-compilation-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!