Issue
I am new to static libraries and just want to make 100% sure that what i'm doing is right. To make it short, i'll try to explain my question with this simple example:
If in mylib.a
i have defined the following functions:
int f1 (int a, int b) {/*some code here...*/}
int f2 (int a, int b) {/*some code here...*/}
int f3 (int a, int b) {/*some code here...*/}
// we also suppose that f1 does not call f2 or f3.
In my project i linked mylib.a
but only used f1
. Would f2
and f3
get into the final executable too?
I have a feeling that this is also compiler specific, but let's consider that we only talk about GCC
here. I'll find any specific information about other compilers like MSVC compiler
and how they treat this problem valuable since i want to make my libraries as compatible as i can.
I also find the explination given on wikipedia vague since i do not find it clear WHO includes those parts of the library...
. Also, the expressionit is enough to include
doesen't give me confidence that only and only the code needed is included.
With static linking, it is enough to include those parts of the library that are directly and indirectly referenced by the target executable (or target library). With dynamic libraries, the entire library is loaded, as it is not known in advance which functions will be invoked by applications.
Solution
Traditionally, when you link with a static library, each object file in the library that satisfies a currently unsatisfied reference will be included in the executable. Any references from the selected object files within the library will also be picked up, repeating until there are no more object files that can satisfy any unsatisfied references. The linking process then moves on to the next library in the list.
If there are still unresolved reference when it reaches the end of the last library, the linker generates error messages about undefined external references.
Answered By - Jonathan Leffler