Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my static library so huge?

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
like image 283
Foglio Avatar asked Jul 26 '10 03:07

Foglio


People also ask

Why are static libraries so big?

Static libraries are much bigger in size, because external programs are built in the executable file. Dynamic libraries are much smaller, because there is only one copy of dynamic library that is kept in memory.

What is a static library file?

In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.

What is the difference between static library and dynamic library?

Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries, on the other hand, exist as separate files outside of the executable file.

Where are static libraries stored?

Static libraries belong next to their corresponding dynamic libraries, and in accordance with the FHS. Keep in mind that static libraries are usually only needed to build software, not run it.


1 Answers

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 inliner to make cross-module decisions and other such optimizations.

Additionally, some compilation modes will embed the debugging symbols directly in the .lib, removing the need for separate .pdb files.

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.

like image 191
Billy ONeal Avatar answered Sep 19 '22 08:09

Billy ONeal