Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do static .lib files link each other?

Tags:

c++

c

For example, if I have a lib A that has dependency on lib B, then do I need to link both lib A and lib B or only lib A, when I write a program C that depends on lib A?

like image 762
ZHOU Avatar asked Mar 05 '15 08:03

ZHOU


2 Answers

Static libraries are not linked. Notice that the tool creating a static library is not a linker—it's ar (archiver) on Unix-like platforms and lib (librarian) when using the MS toolchain. This is an important point to remember when dealing with static libraries and linking.

And it should answer your question. Unless the author of A took explicit extra steps to put the contents of B into A, you'll need to link against both A and B.

like image 139
Angew is no longer proud of SO Avatar answered Nov 12 '22 06:11

Angew is no longer proud of SO


A library (A) that needs another library (B) will link that library to the exe. This is because A doesnt need to know the code in B, only the exe does. This is because in the linking stage, all code in the libraries are effectively copied into the exe, therefore all references to B from A will be resolved at that step.

like image 31
Nonanon Avatar answered Nov 12 '22 07:11

Nonanon