Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linking to multiple versions of a static library

My project links against a static library libA.a version 1.

This executable also links against another static library B. B is linked against A, but version 2.

Both symbol sets in version 1 and 2 of A are the same. Should this work if I know that nothing is shared that has anything to do with A between my project and B?

I managed to link it manually and it seems to work (maybe there are pitfalls I'm not aware of).

I can't make lib B a shared object, or use A as a shared object. Actually, I only know B is using A statically because I talked with the guy who wrote B.

like image 450
user1912594 Avatar asked Dec 18 '12 10:12

user1912594


2 Answers

I stand to be corrected, but it seems you can accomplish what you desire through a careful compilation process and judicious use of objcopy. The steps you should follow are:

  1. Compile version 2 of A.
  2. Compile library B against A2.
  3. Merge B and A2 into a single library C (see this page for related discussion).
  4. Localise all symbols from A2 that are present in C by using objcopy to create C2. Use objcopy --localize-symbols infile outfile, and look here for details.
  5. Compile version 1 of A.
  6. Compile your app (P).
  7. Link P A1 and C2.

I have never done this in practice, but all the building blocks appear to be in place. Step 4 is the hardest, because you have to identify all conflicting symbols -- probably manually.

like image 92
Kevin A. Naudé Avatar answered Sep 28 '22 18:09

Kevin A. Naudé


About the "linking" part of your question, actually static libraries are not linked together. The linking step is only performed to obtain the final executable.

In fact you compiled your static library B using the headers of A (version 2). Then you linked your executable with both the B and A (version 1) libraries.

As far as the symbols are the same in both versions of A, there is no compiling issue. But you have to know that your executable actually only embed & use the version 1 of A.

like image 38
greydet Avatar answered Sep 28 '22 19:09

greydet