Reputation: 205
I'm trying to use two old (and different) usb libraries in a bigger project by encapsulating them into their own namespace like so:
namespace usbA {
#include "/msp430/usb.h"
}
namespace usbB {
#include "/cypress/usb.h"
}
This leads to linker errors obviously because the names in the actual libraries are not decorated with the namespaces as are the function calls. Can this be solved without editing the libraries code? According to some other posters answers this cannot be done. This begs the question of what are namespaces good for then? If I have to go in and modify library source code I can as well prefix each function with "usbA_" instead of using namespace at all. It's equally inflexible.
Upvotes: 2
Views: 183
Reputation: 385194
This leads to linker errors obviously because the names in the actual libraries are not decorated with the namespaces as are the function calls. Can this be solved without editing the libraries code?
I'm sure there will be ghastly ways to hack this, but I would really just … not. In my opinion it's bound to end in tears.
If you really need both libraries, and their names really conflict, I think on balance you're probably best off forking them and giving them namespaces yourself.
This begs the question of what are namespaces good for then?
They're good for nothing if you don't use them (and C code obviously doesn't use them). If these libraries were C++ projects with namespaces, then you wouldn't have this problem, which is good for quite a lot.
It's equally inflexible
Well, this is why C++ introduced namespaces. Sadly you can't force everyone to write C++.
Upvotes: 3