Size of a library and the executable

前端 未结 7 902
半阙折子戏
半阙折子戏 2021-02-14 12:14

I have a static library *.lib created using MSVC on windows. The size of library is say 70KB. Then I have an application which links this library. But now the size of the final

相关标签:
7条回答
  • 2021-02-14 12:39

    Additionally to the current answers, the linker is allowed to remove function definitions if they have identical object code - this is intended to help reduce the bloating effects of templated code.

    0 讨论(0)
  • 2021-02-14 12:41

    @All: Thanks for the pointers. @Greg Hewgill - Your answer was a good pointer. Thanks.

    The answer i found out was as follows:

    1.)During Library building what happens is if the option "Keep Program debug databse" in MSVC (or something alike ) is ON, then library will have this debug info bloating its size. but when i statically include that library and create a executable, the linker strips all that debug info from the library before geenrating the exe and hence the exe size is less than that of the library.

    2.) When i disabled the option "Keep Program debug databse", i got an library whose size was smaller than the final executable, which was what i thought is nromal in most situations.

    -AD

    0 讨论(0)
  • 2021-02-14 12:44

    The static library probably contains several functions which are never used. When the linker links the library with the main executable, it sees that certain functions are never used (and that their addresses are never taken and stored in function pointers), it just throws away the code. It can also do this recursively: if function A() is never called, and A() calls B(), but B() is never otherwise called, it can remove the code for both A() and B(). On Linux, the same thing happens.

    0 讨论(0)
  • 2021-02-14 12:45

    A static library has to contain every symbol defined in its source code, because it might get linked into an executable which needs just that specific symbol. But once it is linked into an executable, we know exactly which symbols end up being used, and which ones don't. So the linker can trivially remove unused code, trimming the file size by a lot. Similarly, any duplicate symbols (anything that's defined in both the static library and the executable it's linked into gets merged into a single instance.

    0 讨论(0)
  • 2021-02-14 12:46

    There is additional bookkeeping information in the .lib file that is not needed for the final executable. This information helps the linker find the code to actually link. Also, debug information may be stored in the .lib file but not in the .exe file (I don't recall where debug info is stored for objs in a lib file, it might be somewhere else).

    0 讨论(0)
  • 2021-02-14 12:55

    Disclaimer: It's been a long time since I dealt with static linking, so take my answer with a grain of salt.

    You wrote: I was thinking it should add directly to the executable size and final exe size should be more than that?

    Naive linkers work exactly this way - back when I was doing hobby development for CP/M systems (a LONG time ago), this was a real problem.

    Modern linkers are smarter, however - they only link in the functions referenced by the original code, or as required.

    0 讨论(0)
提交回复
热议问题