Can you put the library in the namespace?

I work with OpenCV and I would like to put the whole library in my own namespace. I looked around a bit, but could not find an answer ...

Can you do this without changing the source code of the library? If then how?

+6
c ++ namespaces opencv
source share
4 answers

In principle, no. You can try to do this by writing wrappers and macros, but this is unlikely to work. If you really need to do this, the best approach is to fork the library and make the necessary namespace extensions. Of course, you REALLY need to do this in order to take this approach, and I suspect you are not doing this.

+7
source share

Basically, you can write a program that will parse library symbol export tables and change symbol names there. You still have to change the headings, of course.

It would be easier to write a simple script to add namespace tags and recompile the library.

+4
source share

General answer: you cannot, but there are a few tricks you can do.

For example, objcopy from binutils has the ability to copy an object, but place a prefix on each character using - prefix characters . the prefix of things is often poor namespaces of people and is a β€œgood” way to avoid conflicts.

The usage is pretty simple, something like this:

objcopy --prefix-symbols "__mylib_" object.o new_object.o 

NOTE: yes, it also works with .so files.

NOTE 2: this completely violates the C ++ name management, so try this only in the library with the C API. Since you are talking about adding a namespace where it does not exist, I assume that it is.

+3
source share

You can provide a shell header file that declares the same interface inside a namespace. Include the library headers in the source wrapper file and invoke the library. No source outside your source should know about library symbols. If you want to be very careful, you can put all this in a dynamically loaded library.

It was very common to do this with COM to hide the linker dependencies of some library. I can’t understand why you cannot do this with C ++.

+2
source share