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

孤人 提交于 2019-12-09 12:36:17

问题


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?


回答1:


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.




回答2:


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.)




回答3:


Since static libraries do not contain finished binary data, but rather information needed for linker to build binary, this information may be bigger than built binaries.

When some function defined in header file is used in cpp-file, compiler puts its code (either inlines, or simply adds) to resulting object file. This means that there will be a lot of duplicates. It's linker's job to merge them, so static library just waits for linker to be reduced :)

Generally size of executable is usually bigger with static libraries, but size of executable together with dynamic libraries is usually smaller. DLL and EXE are linked separately, so linker cannot know which functionality is needed in DLL and which can be thrown out. In case of static library, linker has such information and can take only those obj-files which are used.




回答4:


The debug static library contains debug information, which explains the huge size difference.



来源:https://stackoverflow.com/questions/4612783/size-difference-between-static-and-dynamic-debug-library-and-impact-on-final-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!