views:

249

answers:

4

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?

+3  A: 

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.

bdonlan
I've done that once to work around ugly name clashes in a third-party library. My strong advice is: **don't**! It's horrible.
Konrad Rudolph
Thanks Konrad from the advice!
Gab Royer
or you could just use objcopy which has the ability to alter symbols :-P.
Evan Teran
@Evan, you still need to figure out how they're mangled on your system
bdonlan
@bdonlan: true ... *if* they are mangled at all. If we are talking about a C style API, then no worries ;). Unfortunately, the OP was non-specific in this regard.
Evan Teran
+5  A: 

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.

anon
Damn it........
Gab Royer
Accepted because it was straight to the point
Gab Royer
And the wisest advice, surely?
anon
+2  A: 

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++.

chrish
+2  A: 

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.

Evan Teran