One of the most important rules and best practices when writing a library, is putting all symbols of the
library into a library specific namespace. C++ makes this easy, due to the namespace
keyword. In
C the usual approach is to prefix the identifiers with some library specific prefix.
Rules of the C standard put some constraints on those (for safe compilation): A C compiler may look at only the first
8 characters of an identifier, so foobar2k_eggs
and foobar2k_spam
may be interpreted as the same
identifiers validly – however every modern compiler allows for arbitrary long identifiers, so in our times
(the 21st century) we should not have to bother about this.
But what if you're facing some libraries of which you cannot change the symbol names / idenfiers? Maybe you got only a static binary and the headers or don't want to, or are not allowed to adjust and recompile yourself.
This is not just an extension of modern compilers; the current C standard also requires the compiler to support reasonably long external names. I forget the exact length but it's something like 31 characters now if I remember right.
Then you're stuck. Complain to the author of the library. I once encountered such a bug where users of my application were unable to build it on Debian due to Debian's
libSDL
linkinglibsoundfile
, which (at least at the time) polluted the global namespace horribly with variables likedsp
(I kid you not!). I complained to Debian, and they fixed their packages and sent the fix upstream, where I assume it was applied, since I never heard of the problem again.I really think this is the best approach, because it solves the problem for everyone. Any local hack you do will leave the problem in the library for the next unfortunate user to encounter and fight with again.
If you really do need a quick fix, and you have source, you could add a bunch of
-Dfoo=crappylib_foo -Dbar=crappylib_bar
etc. to the makefile to fix it. If not, use theobjcopy
solution you found.At least in the case of static libraries you can work around it quite conveniently.
Consider those headers of libraries foo and bar. For the sake of this tutorial I'll also give you the source files
examples/ex01/foo.h
examples/ex01/foo.c (this may be opaque/not available)
example/ex01/bar.h
examples/ex01/bar.c (this may be opaque/not available)
We want to use those in a program foobar
example/ex01/foobar.c
One problem becomes apparent immediately: C doesn't know overloading. So we have two times two functions with identical name but of different signature. So we need some way to distinguish those. Anyway, lets see what a compiler has to say about this:
Okay, this was no surprise, it just told us, what we already knew, or at least suspected.
So can we somehow resolve that identifer collision without modifying the original libraries' source code or headers? In fact we can.
First lets resolve the compile time issues. For this we surround the header includes with a bunch of preprocessor
#define
directives that prefix all the symbols exported by the library. Later we do this with some nice cozy wrapper-header, but just for the sake of demonstrating what's going on were doing it verbatim in the foobar.c source file:example/ex02/foobar.c
Now if we compile this...
... it first looks like things got worse. But look closely: Actually the compilation stage went just fine. It's just the linker which is now complaining that there are symbols colliding and it tells us the location (source file and line) where this happens. And as we can see those symbols are unprefixed.
Let's take a look at the symbol tables with the nm utility:
So now we're challenged with the exercise to prefix those symbols in some opaque binary. Yes, I know in the course of this example we have the sources and could change this there. But for now, just assume you have only those .o files, or a .a (which actually is just a bunch of .o).
objcopy to the rescue
There is one tool particularily interesting for us: objcopy
objcopy works on temporary files, so we can use it as if it were operating in-place. There is one option/operation called --prefix-symbols and you have 3 guesses what it does.
So let's throw this fella onto our stubborn libraries:
nm shows us that this seemed to work:
Lets try linking this whole thing:
And indeed, it worked:
Now I leave it as an exercise to the reader to implement a tool/script that automatically extracts the symbols of a library using nm, writes a wrapper header file of the structure
and applies the symbol prefix to the static library's object files using objcopy.
What about shared libraries?
In principle the same could be done with shared libraries. However shared libraries, the name tells it, are shared among multiple programs, so messing with a shared library in this way is not such a good idea.
You will not get around writing a trampoline wrapper. Even worse you cannot link against the shared library on the object file level, but are forced to do dynamic loading. But this deserves its very own article.
Stay tuned, and happy coding.
If you're using GCC, the --allow-multiple-definition linker switch is a handy debugging tool. This hogties the linker into using the first definition (and not whining about it). More about it here.
This has helped me during development when I have the source to a vendor-supplied library available and need to trace into a library function for some reason or other. The switch allows you to compile and link in a local copy of a source file and still link to the unmodified static vendor library. Don't forget to yank the switch back out of the make symbols once the voyage of discovery is complete. Shipping release code with intentional name space collisions is prone to pitfalls including unintentional name space collisions.