r2d2leboss
r2d2leboss

Reputation: 117

Shared library and multiple levels of link with static libraries

Imagine you have a static lib from a vendor : libvendor.a.

On top of this lib, we built a static library adding new features which uses some symbols of libvendor.a, let's call it libteam.a. So, libteam.a contains some symbols of libvendor.a as libvendor.a is a static library.

Now, we developed our 'components' as shared libraries, let's say libcomponent1.dll, libcomponent2.dll. These components use some features from libteam.a and from libvendor.a, so we linked to libvendor.a and libteam.a.

In the end, we have the executable test.exe using libcomponent1.dll and some features of libvendor.a so we link to both during link step. But, we get the following error multiple times during link :

multiple definition of symbol..., symbols initially defined in libvendor.a but also available in libteam.a (due to link) so the error...

If I remove libvendor.a during link step of libcomponent1.dll library, I get undefined reference to ... error during build of test.exe, as symbols are not available in libteam.a... So I think it's right to link libcomponent1.dll to libvendor.a and libteam.a, fix me if I'm wrong.

I guess we're doing something wrong or maybe we can't do that... The idea is to have a libcomponent1.dll that we can easily distribute and link to applications. People will also surely need libvendor.a, but it's not an issue.

Thank you for your help.

Upvotes: 1

Views: 1992

Answers (1)

John Zwinck
John Zwinck

Reputation: 249093

I can suggest two approaches:

  1. Stop linking libraries to each other. Instead, let your shared libraries have unresolved symbols which will be resolved when they are loaded by your executable which will link the static libraries.

  2. Do "whole-archive" linking of your libraries. Since you're using GCC, the options you need for this are something like:

    gcc -o libteam.a -Wl,--whole-archive -lvendor -Wl,--no-whole-archive ...
    

    This way you will link not just "some" symbols from libvendor.a, but all of them, so you never need to link libvendor.a into anything else that uses libteam.a.

Upvotes: 1

Related Questions