Typedef type name conflict

I have two header files in which I include that both have the same typedef.

Say aaa.h and bbb.h. They came from 2 unrelated third-party packages, and I do not control these 2 files, but they have to use them. I do not include both files directly, but they are more likely included in the top-level header file. (i.e., I include ah and bh, which included them accordingly)

My program will not compile when they are both included due to a name conflict.

So, with my workaround, I copied aaa.h into my source tree with only a remote typedef called myaaa.h. At the top of the file, I keep the shell "#ifndef AAA_H, #define AAA_H" unchanged, so when I turn on myaaa.h, aaa.h will not turn on because the AAA_H flag is already defined, the typedef conflict is indirectly removed.

Is there a more convenient way to do this without adding such a file (and I have to control it) in my source code?

Thanks!

+6
source share
1 answer
#define conflicting_typedef aaa_conflicting_typedef #include "aaa.h" #undef conflicting_typedef #define conflicting_typedef bbb_conflicting_typedef #include "bbb.h" #undef conflicting_typedef #define conflicting_typedef ambiguous use aaa_conflicting_typedef or bbb_conflicting_typedef 

If you need to refer to typedef from aaa.h , you can use the name aaa_conflicting_typedef ; to refer to typedef from bbb.h , use the name bbb_conflicting_typedef . If (as this is probably the case), you don't need to refer to typedef in your code, then you are good to go. The #define final ensures that you are not using a simple name; You must use the correct display name.

Clearly this should not be repeated; if there is more than one file that needs this stanza, you put it in your own “aaa_bbb.h” header and include this where you need the stanza. You can even determine if aaa.h or bbb.h has already been enabled, and reject the compilation if there is one (you cannot determine if they are enabled after that, but the fact that they were included once under your controls , they should not be turned on again).

If it was C ++, you would have a big problem due to type binding. OTOH, library vendors are then guilty of mismanaging the namespace.

+10
source

Source: https://habr.com/ru/post/924682/


All Articles