Typedef, Violation of ODR: same keyword alias different types

273 Views Asked by At

I need to link 2 libraries. The first library requires a header file in which:

typedef int TYPE

The second library requires a header file in which:

typedef struct type TYPE

Obviously linking them results in a violation of the One Definition Rule (ODR). The obvious solution here is to rename one of the types. However, this will require me to change that in hundreds of files too, which I'm trying to avoid because it will make all of our projects inconsistent. Is there any other way to overcome this issue?

1

There are 1 best solutions below

0
On BEST ANSWER

If library A has the header file header_a.h and library B has header file header_b.h you can do something like this as a workaround in source code forms that have to include both:

#define TYPE TYPE_A
#include <header_a.h>
#undef TYPE

#define TYPE TYPE_B
#include <header_b.h>
#undef TYPE

This causes the declarations

typedef int TYPE;          /* from library A */
typedef struct type TYPE;  /* from library B */

to appear as

typedef int TYPE_A;
typedef struct type TYPE_B;

This should work fine as type information is not present in object file. Nonetheless, the header files from libraries A and B might do weird stuff with macros that break with these definitions, so you should check for that before.