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 c开发者_如何学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
, and myapp
to make things a little more legible:
Suppose you have static library
libstatic
, and dynamic shared object librarylibshared
and your programmyapp
under linux. Suppose that librarylibstatic
calls functions from librarylibshared
and your program (myapp
) calls functions from librarylibstatic
. Now suppose that all functions thatmyapp
calls inlibstatic
make no use of functions inlibshared
.To compile
myapp
will it be enough to link justlibstatic
and omitlibshared
and furthermore can your programmyapp
be run on a system without librarylibshared
installed?
So the way I understand your question, there is a library libstatic
, some functions in which make use of libshared
. You want to know: if I don't use any of the libstatic
functions that are dependent on libshared
, will myapp
link and run without libshared
?
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 from libstatic
depend on libshared
, then myapp
doesn't depend on libshared
.
精彩评论