views:

183

answers:

2

I have a C++ compiled static library of about 15 classes and their member functions and stuff, and compiled, it's almost 14 megabytes. It links to Google's dense hash table library and MPIR, which is like GMP for Windows, but I did that in a plain exe and it was a few kilobytes. Why is it so massive? What can I do to reduce its size? It seems like there's other programs which are far more complicated than mine that are far smaller.

Compiled with Visual C++, command line is:

/Zi /nologo /W3 /WX- /O2 /Oi /Oy- /GL /D "WIN32" /D "NDEBUG" /D "_CONSOLE"
/D "_UNICODE" /D "UNICODE" /Gm- /EHsc /GS /Gy /fp:precise /Zc:wchar_t
/Zc:forScope /Fp"Release\ang.pch" /Fa"Release\" /Fo"Release\"
/Fd"Release\vc100.pdb" /Gd /analyze- /errorReport:queue
+6  A: 

The static library is a considerably different format the finished binary; partially because it has quite a bit more information. Essentially, the static library acts like a ZIP of all the .obj files generated from your translation units. This allows the linker to throw out unused functions, and if you're using LTCG, it allows the backend to make cross-module inlineing decisions and other such optimizations.

Additionally, some compilation modes will embed the debugging symbols directly in the .lib.

Generally you shouldn't need to worry about static library size; the linker will throw out all the extra information when building the final executable.

Billy ONeal
The linker also throws out redundant and dead code, etc.
greyfade
@greyfade: Actually the compiler does that ;)
Billy ONeal
@Billy ONeal: At *link time*?
greyfade
@greyfade: No, that's done at compile time, unless you use LTCG, in which case the linker is actually the compiler back end.
Billy ONeal
@Billy ONeal: GCC produces large object files. It does no code removal, and emits nothing suitable for LTCG. (It *has* no such feature.) Binutils' `ld`, on the other hand, takes the object files and strips them of redundant and dead code while it performs relocations and linking. No LTCG in sight.
greyfade
@greyfade: It must be doing LTCG then; it's just not calling it that. A true linker doesn't know anything about the code it's looking at other than performing function fixups.
Billy ONeal
@Billy ONeal: They call it "garbage collection" http://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep It only knows about symbols and whether they're referenced.
greyfade
Thanks, very informative answer.
Foglio
@greyfade: If all it knows about are symbols, how can it remove dead code? Dead code doesn't affect symbol tables. Sure, it removes unreferenced functions, but it's not doing full DCE.
Billy ONeal
+1  A: 

I think that you should adjust your perceptions. Fourteen megabytes is not huge.

wilx