Reputation: 51
What is the easier way to reduce library size by selecting only needed functions and eliminating unnecessary files ? Is there a script to accomplish this task for c++ libraries?
Upvotes: 2
Views: 10082
Reputation: 450
1, using lto
2, using version script to control which symbols are exported
3, use
CFLAGS="-Os -ffunction-sections -fdata-sections
-fno-unwind-tables -fno-asynchronous-unwind-tables -flto"
LDFLAGS="-Wl,-s -Wl,-Bsymbolic -Wl,--gc-sections"
4, strip library
5, use -Oz with clang or -Os with gcc compiler.
Upvotes: 0
Reputation: 1
You probably should first try to set up your compilation to minimize size. And the answer to your question depends a lot on the compiler, the linker, what operating system, the optimization flags, etc...
With a recent GCC compiler (g++
) on Linux, you should try to compile and link wih optimization for size (-Os
) and link time optimization (-flto
).
So put
CXX=g++ -flto -Os
near the beginning of your Makefile
or simply run make CXX='g++ -flto -Os'
after a make clean
BTW, Clang/LLVM also knows about -flto
(and uses GOLD like GCC does)
Notice that a shared library (or probably a DLL in Microsoft world) needs to contain all the code (precisely because it is shared between several processes and programs). You might link your library statically (then use g++ -flto -Os
both when building the static library and when linking it to the main program)
Very often, having shared libraries is more worthwhile than trying to reduce their space.
If on Linux, read the program library howto.
Upvotes: 3
Reputation: 129524
In typical C++ applications, templates stand for a large portion of the functionality (e.g. STL vector
, string
, etc). These functions are ONLY generated as code when they are used (although they MAY be generated as inline functions, which in SOME cases can lead to large code).
The linker will only "pick" code that is needed for your application (including of course, functions called by functions that you use). However, the base runtime on for example Linux is quite large, because it calls a lot of functionality that in turn uses a fair bit of other function - so your basic executable size is quite large. Adding more of your own code will not increase the size by a significant amount in the typical case.
If size is critical, then using a "small C++ library" would perhaps be a choice - there are different such libraries available for different OSes. Using options such as -Os
for the compiler to tell it to "make the code small" (in other words, don't inline functions unless the code is shorter by inlining than by calling the function, and don't unroll loops, etc, etc - but DO inline functions called only once, as that does make the code shorter)
Upvotes: 2
Reputation: 8325
If you don't want to do a library heavy refactoring (assuming you have access to sourcecode wich is not always a realistic hypotesis), is to link it as static library, or compile as part of your application: all compilers will benefit from that.
Note that Clang has a exceptional behaviour in the sense it is able to do a lot of optimizations at static linking time compared to other compilers.
If you instead have to use a DLL for some reason, you can try to tweak visibility of exported symbols (you can hide symbols not visible in client code and export only symbols you use): note that this may enable some optimization regardin speed and binary size but could be a tedious work (I would count it as a major refactoring because has the potential to break things).
If you are using dependency injection containers you can probably instrument them to warn about unused dependencies so that you can easily wire them out without ever the need of touching source code (you do some refactoring of the container but not of your whole codebase) except source code in composition root.
Upvotes: 0