Checking when two headers are included at the same time
I need to do an assertion based on two related macro preprocessor #define
's declared in different header files... The codebase is huge and it would be nice if I could find a place to put the assertion where the two headers are already included, to avoid polluting namespaces unnecessarily.
Checking just that a file includes both explicitly might not suffice, as one (or both) of them might be included in an upper level of a nesting include's hierarchy.
I know it wouldn't be too hard to write an script to check that, but if there's already a tool that does the job, the better.
Example:
file foo.h
#define FOO 0xf
file bar.h
#define BAR 0x1e
I need to put somewhere (it doesn't matter a lot where) something like this:
#if (2*FOO) != BAR
#error "foo is not twice bar"
#endif
Yes, I know the example is silly, as they could be replaced so one is derived from the other, but let's say that the includes can be generated from different places not under my control and I just need to check that they match at compile time... And I don't want to just add one include after the other, as it might conflict with previous code that I haven't written, 开发者_高级运维so that's why I would like to find a file where both are already present.
In brief: how can I find a file that includes (direct or indirectly) two other files?
As you have both of your includes probably protected by #define guards, you might check in each of those two header files if the other one is included based on #ifdef THE_OTHER_GUARD_NAME
, and issue warning (or error, whichever you prefer) if you detected it is included.
If you are on a Unix or Unix-alike system, there is a utility called makedepend
you can use.
http://en.wikipedia.org/wiki/Makedepend
If you are on Windows, there's a Sourceforge project called makedep that might work.
What about #pragma once operator? After that you may forget to worry about multiply header file definitions...
精彩评论