2

I've tried passing -ffunction-sections -fdata-sections for the compiler, but that doesn't seem to have the desired effect. As far as I understand, I also have to pass -Wl,--gc-sections to the linker, but I'm not linking the files at this point. I just want to have a .a library file as small as possible, with minimal redundant code/data.

Paul
  • 6,061
  • 6
  • 39
  • 70
  • Without having the final executable, what code do you expect to be removed from static library? Unused code may be removed only when executable is linked with a static library. It looks like the question has no answer. – Alex F Sep 02 '14 at 09:37
  • @AlexFarber the thing is, while playing with the command line arguments of the compiler, I remember being able to minimize the size of the `.a` file by ~50%, but I cannot reproduce it now. The executable file was smaller too, and it worked, so there must be some combination which results in a smaller static library file. – Paul Sep 02 '14 at 21:14
  • `-Os -fvisibility=hidden -fomit-frame-pointer -mno-accumulate-outgoing-args -finline-small-functions -s` ; however the `-f*-sections` and `-flto` flags will make your static library larger (but the resulting binary smaller) ... I would provide a full answer if I didn't think the question itself is misguided. The only way to really make a static library smaller is to compile the final product with `--print-gc-sections` to find out what parts are cruft. Then go back and remove those functions/variables (this also works for making smaller shared libraries - see http://libraryopt.sf.net) – technosaurus Sep 05 '14 at 02:58
  • Thank you @technosaurus, `-s` is exactly what I needed! The library has gone 244 KB -> 157 KB, and the executable: 274 KB -> 193 KB. Please post an answer so I could give you the bounty. – Paul Sep 05 '14 at 09:36

3 Answers3

2

The compiler performs optimization based on the knowledge it has of the program. Optimization levels -O2 and above, in particular, enable unit-at-a-time mode, which allows the compiler to consider information gained from later functions in the file when compiling a function. Compiling multiple files at once to a single output file in unit-at-a-time mode allows the compiler to use information gained from all of the files when compiling each of them.

Not all optimizations are controlled directly by a flag.

-ffunction-sections -fdata-sections Place each function or data item into its own section in the output file if the target supports arbitrary sections. The name of the function or the name of the data item determines the section's name in the output file.

Use these options on systems where the linker can perform optimizations to improve locality of reference in the instruction space. Most systems using the ELF object format and SPARC processors running Solaris 2 have linkers with such optimizations. AIX may have these optimizations in the future.

Only use these options when there are significant benefits from doing so. When you specify these options, the assembler and linker will create larger object and executable files and will also be slower. You will not be able to use gprof on all systems if you specify this option and you may have problems with debugging if you specify both this option and -g. 

U can use the following link for more details..:-) http://gcc.gnu.org/onlinedocs/gcc-4.0.4/gcc/Optimize-Options.html

m2pathan
  • 360
  • 2
  • 17
1

The following will reduce the size of your compiled objects (and thus the static library)

-Os -g0 -fvisibility=hidden -fomit-frame-pointer -mno-accumulate-outgoing-args -finline-small-functions -fno-unwind-tables -fno-asynchronous-unwind-tables -s

The following will increase the size of objects (though they may make the binary smaller)

-ffunction-sections -fdata-sections -flto -g -g1 -g2 -g3

The only way to really make a static library smaller is to remove the unneeded code by compiling the static library with -ffunction-sections -fdata-sections and linking the final product with -Wl,--gc-sections,--print-gc-sections to find out what parts are cruft. Then go back and remove those functions/variables (this also works for making smaller shared libraries - see http://libraryopt.sf.net)

Peter O.
  • 32,158
  • 14
  • 82
  • 96
technosaurus
  • 7,676
  • 1
  • 30
  • 52
0

Compare the size of the library if you -g in the compiler flags, vs not having it. I can easily imagine you double the size of a static library if it includes debug information in. If that was what you saw, chances are you already stripped the library of debug symbols, and hence cannot significantly shrink the library file more. This could explain your memory of cutting the size in half at some point.

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173