tags:

views:

901

answers:

9
+11  Q: 

C function conflict

What should I do if I have two libraries that provide functions with equivalent names?

+1  A: 

Swear? As far as I am aware, there isn't much you can do if you have two libraries that expose link points with the same name and you need to link against both.

Vatine
I was hoping there is a solution...
qeek
I think this is sort of a Kobyashi Moru...
Uri
Swear is *definitely* the first step. No doubt about it.
dmckee
+3  A: 

You should not use them together. If I remember correctly, the linker issues an error in such a case.

I didn't try, but a solution may be with dlopen(), dlsym() and dlclose() which allow you to programmatically handle dynamic libraries. If you don't need the two functions at the same time, you could open the first library, use the first function and close the first library before using the second library/function.

mouviciel
Thanks. Didn't think about this. Although, I'd like to have both at the same time.
qeek
+17  A: 
  • If you control one or both: edit one to change the name and recompile Or equivalently see Ben and unknown's answers which will work without access to the source code.
  • If you don't control either of them you can wrap one of them up. That is compile another (statically linked!) library that does nothing except re-export all the symbols of the original except the offending one, which is reached through a wrapper with an alternate name. What a hassle.
  • Added later: Since qeek says he's talking about dynamic libraries, the solutions suggested by Ferruccio and mouviciel are probably best. (I seem to live in long ago days when static linkage was the default. It colors my thinking.)

Apropos the comments: By "export" I mean to make visible to modules linking to the library---equivalent to the extern keyword at file scope. How this is controlled is OS and linker dependent. And it is something I always have to look up.

dmckee
You stole the answer out of my mouth. +1
toast
Luck of the reload, or something.
dmckee
That was my first thought as well, but won't you end up with the same collision problem? In the end, the entire project has to link - at compile/link time or at run time - at which time both the offending libraries have to load as-is.
@unknown: The wrapper *must* be compiled with static linkage, and should not export the offending symbol. Then you can still dynamically link the wrapper. Edited for more clarity, Thanks.
dmckee
If qeek's problem is with ddl's and not static libraries, how is it possible to make a new library with a wrapper? Since, the wrapper library would have to dynamically wrap around a function in the library you don't want to link with in the first place.
jeffD
@dmckee - what do you mean by "export"?
anon
@Neil: Expose. Make visible to modules linking to the library. Equivalent to extern and file level in c.
dmckee
@dmckee all functions are extern by default and thus already exposed - I can't see how you can alter this for a precompiled library
anon
@Neil: It is a function of the linker. You can generally tell your linker to construct library C out of code A and library B *without* exposing the symbols from B. Or exposing only selected symbols. How to specify it is system dependent.
dmckee
i think you can do it with visibility=hidden with gcc/ld
Johannes Schaub - litb
perhaps someone could provide a simple example of this technique? One exe, two libraries each containing one function with the same name.
anon
+3  A: 

This problem is the reason c++ has namespaces. There's not really a great solution in c for 2 third party libs having the same name.

If it's a dynamic object, you might be able to explicitly load the shared objects (LoadLibrary/dlopen/etc) and call it in that fashion. Alternately, if you don't need both libs at the same time in the same code, you can maybe do something with static linking (if you have the .lib/.a files).

None of these solutions apply to all projects, of course.

Brian Mitchell
A: 

I've never used dlsym, dlopen, dlerror, dlclose, dlvsym, etc., but I'm looking at the man page, and it gives an example of opening libm.so and extracting the cos function. Does dlopen go through the process of looking for collisions? If it doesn't, the OP could just load both libraries manually and assign new names to all the functions his libraries provide.

+1  A: 

You should write a wrapper library around one of them. Your wrapper library should expose symbols with unique names, and not expose the symbols of the non-unique names.

Your other option is to rename the function name in the header file, and rename the symbol in the library object archive.

Either way, to use both, it's gonna be a hack job.

James Caccese
+5  A: 

Here's a thought. Open one of the offending libraries in a hex editor and change all occurrences of the offending strings to something else. You should then be able to use the new names in all future calls.

UPDATE: I just did it on this end and it seems to work. Of course, I've not tested this thoroughly - it may be no more than a really good way to blow your leg off with a hexedit shotgun.

actually not a terrible solution. A bit hackish, but all you'd be doing is changing the strings in the symbol table. No real functional harm in that.
Evan Teran
You'd probably want to rename the library, as well - lest someone else came along, trying to load the thing again. You'd go from one conflict to dozens or hundreds. =] I love this about stackoverflow: we have a tested answer to a question and it has 3 votes. The first (incomplete) answer: 17. =]
+9  A: 

It is possible to renames symbols in an object file using objcopy --redifine-sym old=new (see man objcopy).

Then just call the functions using their new names and link with the new object file.

Ben
great answer, +1
Evan Teran
Nice. This would be trivial to add to a Makefile. If the libraries are ever updated, an objcopy incantation would be much easier to update than some of the other solutions.
sigjuice
Don't forget to rename the symbols in the header files as well.
mouviciel
+4  A: 

Under Windows, you could use LoadLibrary() to load one of those libraries into memory and then use GetProcAddress() to get the address of each function you need to call and call the functions through a function pointer.

e.g.

HMODULE lib = LoadLibrary("foo.dll");
void *p = GetProcAddress(lib, "bar");
// cast p to the approriate function pointer type (fp) and call it
(*fp)(arg1, arg2...);
FreeLibrary(lib);

would get the address of a function named bar in foo.dll and call it.

I know Unix systems support similar functionality, but I can't think of their names.

Ferruccio
It's dlopen() in UNIX :) That has been answered previosuly.
qeek