Force to link against unused shared library
It looks like you need either -Wl,--no-as-needed
to totally disable it. Or, --no-as-needed -lfoo --as-needed
to disable "as-needed" just for libfoo
.
Source: https://lists.ubuntu.com/archives/ubuntu-devel/2010-November/031991.html
How to force gcc to link an unused static library
Use --whole-archive
linker option.
Libraries that come after it in the command line will not have unreferenced symbols discarded. You can resume normal linking behaviour by adding --no-whole-archive
after these libraries.
In your example, the command will be:
g++ -o program main.o -Wl,--whole-archive /path/to/libmylib.a
In general, it will be:
g++ -o program main.o \
-Wl,--whole-archive -lmylib \
-Wl,--no-whole-archive -llib1 -llib2
How to force gcc to link unreferenced, static C++ objects from a library
You can use -Wl,--whole-archive -lyourlib
, see the manpage for ld
for more info.
Any static libraries mentioned after -Wl,--whole-archive on the command line gets fully included, you can turn this off again too if you need to , as in e.g. -Wl,--whole-archive -lyourlib -Wl,--no-whole-archive -lotherlib
Is C++ linkage smart enough to avoid linkage of unused libraries?
Including or linking against large libraries usually won't make a difference unless you use that stuff. Linkers should perform dead code elimination and thus ensure that at build time you won't be getting large binaries with a lot of unused code (read your compiler/linker manual to find out more, this isn't enforced by the C++ standard).
Including lots of headers won't increase your binary size either (but it might substantially increase your compilation time, cfr. precompiled headers). Some exceptions stand for global objects and dynamic libraries (those can't be stripped). I also recommend to read this passage (gcc only) regarding separating code into multiple sections.
One last notice about performances: if you use a lot of position dependent code (i.e. code that can't just map to any address with relative offsets but needs some 'hotpatching' via a relocation or similar table) then there will be a startup cost.
Related Topics
Concatenate Two String Literals
Qt: Resizing a Qlabel Containing a Qpixmap While Keeping Its Aspect Ratio
When Is It Best to Use the Stack Instead of the Heap and Vice Versa
Redirect Stdout/Stderr to a String
Does There Exist a Static_Warning
How Do Sizeof(Arr)/Sizeof(Arr[0]) Work
Is the Order of Iterating Through Std::Map Known (And Guaranteed by the Standard)
How to Use Std::Sort to Sort an Array in C++
How to Use C++ Classes with Ctypes
Send and Receive a File in Socket Programming in Linux with C/C++ (Gcc/G++)
When G++ Static Link Pthread, Cause Segmentation Fault, Why
How to Simulate "Press Any Key to Continue"
Is There Any "Standard" Htonl-Like Function for 64 Bits Integers in C++
How to Properly Replace Global New & Delete Operators
Difference Between Char* and Char[]
How Are Iterators and Pointers Related
What Expressions Yield a Reference Type When Decltype Is Applied to Them