views:

43

answers:

3

Hi,

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?

Thanks!

A: 

What about #pragma once operator? After that you may forget to worry about multiply header file definitions...

mosg
that doesn't solve my problem, I don't want to prevent multiple inclusions, I just want to find a place to write an assertion that depends on two different includes
fortran
-1 for choosing a platform specific pragma
elcuco
@elcuco "-1" to you, because author didn't point that he need platform independent answer...
mosg
+1  A: 

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.

Suma
Clever trick to use the prerocessor itself to do get the job done! :-) one of them doesn't have the guards, but I can add it temporarily and compile to find where it complains...
fortran
+1  A: 

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.

JeremyP