Mastering Development

Do all functions from a static library get linked into the final executable?

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.

Leave a Reply

Your email address will not be published. Required fields are marked *