I have a question that is very closely related to Linker does not emit multiple definition error when same symbol coexists in object file and static library, but concerns a slightly different situation.
Just like there, I have two .cpp files (say: test1.cpp
and test2.cpp
) each containing an implementation of the same function, i.e. void testfunc()
. I also have a header file test.h
where I declare testfunc
, as well as a file main.cpp
with a main function which contains a call to testfunc()
like this:
include "test.h"
int main() {
testfunc();
}
I compile the .cpp files separately by invoking g++ -c *.cpp
and then create a static library out of them with ar rvs libtest.a test1.o test2.o
. When linking main.o
against the library now, the linker does not complain as I expected it to do:
gcc main.o -L. -ltest -o main
The resulting executable works perfectly fine - calling one of the two implementations of testfunc()
. To be honest, I expected some error like multiple definition of...
to occur. Therefore, my questions are:
ar
, which only adds one of the two object files to the library, or does the library contain both object files & the reason for this behavior is rather to be found in the linking process, where the linker stops searching the lib after it found one definition of testfunc
?testfunc
is actually used or is this even defined? I.e., is it maybe the order of the arguments to ar
deciding which one is used?ar
or might it be that this depends on the system?For static libraries recompilation is required if any changes were applied to external files. On other hand for Shared libraries there is no need to recompile the executable. Static libraries take longer to execute, because loading into the memory happens every time while executing.
Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries, on the other hand, exist as separate files outside of the executable file.
Static libs are nothing more than containers for definitions in a format which is outside the standard. So, this has the following implications: 1 ) I don't quite get what you're asking here, but one static library can very much depend on another static library.
Static Linking and Static Libraries is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory.
A library is just a collection of object-files containing exported symbols. It may contain any number of duplicates (just like real book libraries may contain many books with the same title). There is no linking involved.
When linking, generally speaking, the linker only looks at libraries in case there are unresolved symbols. When looking for those, it might find one and if it does it will look no further for that symbol.
There may arise a conflict when it resolves another unresolved symbol found in another object file that contains a definition of an earlier found symbol; now it will generate an error of duplicate symbols.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With