I am working with OpenCV and I would like to put the entire library inside it's own namespace. I've looked around quite a bit but haven't found an answer...
Can you do this without modifying the library source code? If then how?
I am working with OpenCV and I would like to put the entire library inside it's own namespace. I've looked around quite a bit but haven't found an answer...
Can you do this without modifying the library source code? If then how?
You could in principle write a program that would parse the library's symbol export tables, and alter the names of symbols there. You'd still need to change the headers of course.
That said it would be much easier to write a simple script to add namespace tags and recompile the library.
Basically no. You could attempt to do it by writing wrappers and macros, but it would be unlikely to work. If you really need to do this, a better approach is to fork the library and make the needed namespace additions. Of course, you would REALLY need to do it to take this approach, and I suspect you don't.
You could provide a wrapper header file that declares the same interface inside a namespace. In your wrapper source file, include the headers for the library and call into that library. No source outside your source needs to know about the library's symbols. If you want to be really careful, you can put that all inside a dynamically loaded library.
It was very common to do this with COM to hide the linker dependencies of some library. Can't see why you can't do it with C++.
The general answer is that you can't, but there is a few tricks you can do.
For example, objcopy from binutils has the ability to copy an object, but put a prefix on every symbol by using the --prefix-symbols flag. prefixing things is often the poor man's namespaces and is an "ok" way of avoiding conflicts.
Usage is pretty simple, something like this:
objcopy --prefix-symbols "__mylib_" object.o new_object.o
NOTE: yes, it does work with .so files too.
NOTE 2: this will completely break c++ name mangling, so only attempt this on a library with a C style API. Since you are talking about adding a namespace where there is none, I assume this is the case.