Reputation: 113
I am building my project in C++. I have these two libraries: shared_lib1 and static_lib2. Both have a function with the same name as func(). Only func() in static_lib2 has an assert statement. shared_lib1 is a shared library and static_lib2 is a static library.
and lib3 has dependency on shared_lib1.
I can't change any of these two libs: shared_lib1, static_lib2, because they are third-party libraries. lib3 is available either as a dynamic library or a static library.
I use lib3 as a shared library: shared_lib3. When this shared_lib3 was built, ldd command shows its dependency on shared_lib1. So I think shared_lib3 should use func() in shared_lib1.
In my project, it links to both shared_lib3 and static_lib2, and src1.cpp calls a function func3() in shared_lib3, which is supposed to call func() in shared_lib1, but the program aborted with a core dump and the backtrace shows it aborted because of the assert statement in func() from static_lib2.
Upvotes: 1
Views: 615
Reputation: 213456
So I think shared_lib3 should use
func()
in shared_lib1.
That is not how shared libraries work on UNIX. See this answer.
When you link static_lib2
into the main executable, the func()
defined there gets to the head of the queue, and will be used to resolve all calls to func()
regardless of where these calls come from. This is one of the major differences between UNIX shared libraries and windows DLLs.
There are a few things you may try to fix this symbol collision problem.
If you are using Linux or another ELF platform, you may be able to "hide" the func()
symbol in the main executable. See man objcopy and --localize-symbol
. This will work if nothing calls func()
outside the .o
file (part of static_lib2
) which defines that function.
If that doesn't work, you could try to turn the func()
definition into a HIDDEN symbol (which will prevent it from being exported from the main executable). See this answer and the answer it references.
If that doesn't work, objcopy
can also rename the func()
symbol to a different name -- see --redefine-sym
flag.
Upvotes: 2