I'm modifying a large C++ project, which defines in one of its main headers an enum FooBar. That enum gets included everywhere, and sadly is not namespaced.
From that project I'd like to use a C library, which unfortunately also defines an enum FooBar in the same global namespace.
I can't change the library implementation, and it's difficult to rename or namespace the enum in the C++ project because it's used all over the place.
So ideally I would add a namespace to all symbols coming from the C library. I have tried something like:
namespace c_library_foo {
#include <c_library_foo.h>
}
...
c_library_foo::c_library_function()
...
and that works fine as far as compilation is concerned, but of course the linker then fails to resolve the symbols from the library as the namespace is not in the implementation.
Well I found the solution about 2s after posting this. Adding extern "C" makes it drop the namespace when resolving the symbols, and fixes my problem. Something like:
namespace c_library_foo {
extern "C" {
#include <c_library_foo.h>
}
}
...
c_library_foo::c_library_function()
...
Nope. Supporting namespaces in C++ requires name mangling. The symbols emitted by the C library aren't name mangled (beacuse that doesn't happen in C). You need to rename the C++ enum rather than the C enum.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With