Do I need to link statically linked libraries with their dependencies?
I am creating an executable that depends on static library A, which in turn depends on static library B. When I build my application, do I need to link it to B in my build script as well?
To be more specific, do I have to do -la -lb
, or just linking to A through is -la
enough?
You may need to link to both libraries depending on how A. was built.
If A contains a linker comment entry indicating that the linker also looks in library B for symbols (usually included in one of the object files contained in A), then you do not need to include B when linking. If A does not contain this comment entry, you must enable it yourself.
If both A and B are static, then you must concatenate both, in order A, then B ( -la -lb
). See this answer for an explanation of the order.
A statically linked program includes the libraries linked to it inside the executable.
Imagine that your program calls foo()
inside A, and somewhere inside A is bar()
called. So if A becomes part of your program, you then call undefined in bar()
in your program, so you also need to set a reference to B.
The exception is that a custom Visual Studio pragma ( #pragma comment(lib, libname)
) is used as @ 1201ProgramAlarm pointed out.
The static library is fully included in the used program at compile time, so no additional file is required to run the program.
If library A was already built with static library B, A already has B and will no longer be needed.