I've a program which links to many libraries. g++
, by default, prefers to link to shared libraries, even if the corresponding archive exists.
How can I change this preference to prefer static archives over dynamic libraries, if a static archive exists?
Note, I used -static
option, but it tries to find static archive for all libraries which is not what I want.
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.
Static libraries are either merged with other static libraries and object files during building/linking to form a single executable or loaded at run-time into the address space of their corresponding executable at a static memory offset determined at compile-time/link-time.
You can't statically link a shared library (or dynamically link a static one).
The main difference between static and dynamic linking is that static linking copies all library modules used in the program into the final executable file at the final step of the compilation while, in dynamic linking, the linking occurs at run time when both executable files and libraries are placed in the memory.
g++ -Wl,-Bstatic -lz -lfoo -Wl,-Bdynamic -lbar -Wl,--as-needed
Will link zlib
and libfoo
as static, and libbar
as dynamic . --as-needed
will drop any unused dynamic library.
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