Can I mimic a C header that redefines bool in C++?

c, c++, mixing, typedef

Solution

You can hack it!

The library, call it `fooLib`, thinks it's using some type `bool` which it has the prerogative to define. To the library, `bool` is just an identifier.

So, you can just force it to use another identifier instead:

#define bool fooLib_bool
#include "fooLib.h"
#undef bool
#undef true
#undef false

Now the compiler sees the offending line transformed to this:

typedef int fooLib_bool;

You're stuck with the interface using type `fooLib_bool = int` instead of a real `bool`, but that's impossible to work around, as the code might in fact rely on the properties of `int`, and library binary would have been compiled with such an assumption baked in.

Problem

I am writing a program and I would really prefer to write in C++, however, I'm required to include a C header that redefines bool: ``` # define false 0 # define true 1 typedef int bool; ``` The obvious solution would be to edit the header to say: ``` #ifndef __cplusplus # define false 0 # define true 1 typedef int bool; #endif ``` but, alas, since the library is read-only I cannot. Is there a way I can tell gcc to ignore this typedef? Or, can I write most functions in C++ and then make a C wrapper for the two? Or, should I suck it up and write the thing in C?

Original source

Related problems