I have a question which may be somewhat silly because I'm pretty sure I may know the answer already.
Suppose you have static library A, and dynamic shared object library B and your program C under linux. Suppose that library A calls functions from library B and your program calls functions from library A. Now suppose that all functions that C calls in A make no use of functions in B.
To compile C will it be enough to link just A and omit B and furthermore can your program C be run on a system without library B installed?
If your program calls functions in A that don't reference B then B is not required either at link or load time, assuming that the functions in A are in separate compilation units, which is usually the case for a library.
The linker will pull the functions from the library that C uses and since none of them call functions in B, B will not be needed.
Holy placeholder name overload, batman. Let's first replace A, B, and C, with
libstatic
,libshared
, andmyapp
to make things a little more legible:So the way I understand your question, there is a library
libstatic
, some functions in which make use oflibshared
. You want to know: if I don't use any of thelibstatic
functions that are dependent onlibshared
, willmyapp
link and run withoutlibshared
?The answer is yes, so long as two things are true:
The calls you make into
libstatic
do not depend onlibshared
directly or indirectly. Meaning that ifmyapp
calls a function inlibstatic
which calls another function inlibstatic
which calls a function inlibshared
, thenmyapp
is now dependent onlibshared
.The calls you make into
libstatic
do not depend on any function inlibstatic
whose implementation appears in the same compilation unit (object file) with a call tolibshared
. The linker brings in code from the static library at the level of object files, not at the level of individual functions. And remember, this dependency is similarly chained, so if you call a function infoo.o
, and something else infoo.o
calls a function inbar.o
, and something inbar.o
depends onlibshared
, you're toast.When you link in a static library into an application, only the object files that contain the symbols used (directly or indirectly) are linked. So if it turns out that none of the object files that
myapp
ends up needing fromlibstatic
depend onlibshared
, thenmyapp
doesn't depend onlibshared
.