Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size difference between static and dynamic (debug) library and impact on final exe

I never put much thought into the size difference between a static library and a dynamic library until I downloaded pre-built libraries of boost today. I found that the static libraries of boost are much much bigger than the dynamic libraries.

For example, the debug multi-threaded boost wave static library is 97.7 mb in size while the same library, but dynamic, is only 1.4 mb in size (including import library and dll)! That is a huge difference. Why is that?

Second question, if I statically link against, let's say, the wave library. Does that mean my executable will balloon in size to more than 97.7 mb?

like image 347
Samaursa Avatar asked Jan 06 '11 07:01

Samaursa


People also ask

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.

Why is the statically linked executable so much larger than the dynamically linked executable?

Statically linked files are significantly larger in size because external programs are built into the executable files. In dynamic linking only one copy of shared library is kept in memory. This significantly reduces the size of executable programs, thereby saving memory and disk space.

What are a few relevant differences between the use of static and dynamic libraries?

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. Executable file will have to be recompiled if any changes were applied to external files.

Which is better static or dynamic library?

Static: takes longer to execute, because loading into the memory happens every time while executing. Dynamic: faster, because shared library code is already in memory.


2 Answers

The static libraries have the full debug symbol information in them. For DLLs that information would be in .pdb files (which I assume would be similar in size to the static libs).

When you link to the static lib, the symbol information will not be copied into the .exe - it will be placed in the .pdb file (if your build is configured to create a .pdb file). The .pdb file does not need to be distributed with the .exe, whether or not the .pdb is created.

In the pre-built library download I get from boostpro.com, I don't get .pdb files for the boost DLLs they provide. if you build the DLLs yourself, you'll probably get the .pdb files (though you might have to set some config option, for which I have no idea what the details are).


update:

Looks like I might be wrong about easily getting .pdb files for the boost DLLs. From http://comments.gmane.org/gmane.comp.lib.boost.build/23246:

> Is there an additional option that I can pass on the command line to
> have the (correctly generated) PDB files also copied into the stage
> directory?

Not at this time. You can only hack tools/build/v2/tools/package.jam to add <install-type>PDB everywhere where <install-type>SHARED_LIB or <install-type>STATIC_LIB is now written.

like image 103
Michael Burr Avatar answered Oct 16 '22 03:10

Michael Burr


No, just because the LIB file is a certain size, doesn't mean it will add that size to your EXE. In fact, most linkers are smart enough to link in only the stuff that's used. Compare that to a dynamic library, which must contain everything.

Static libraries definitely make your EXE larger, but I always prefer it. Then I don't have to worry about missing or incompatible libraries at run time. (Or at least, I minimize the chances of this.)

like image 35
Jonathan Wood Avatar answered Oct 16 '22 02:10

Jonathan Wood