I am statically linking to a 1开发者_如何学JAVA0MB lib and included a header file and just used some definitions. I was surprised to see that my executable size is only 100k!
I guess the linker only links in the functions/variables that are used in the exe? Is there any way to predict an executable size given x size of lib?
Or is it purely size of lib implementation eg of every function used?
Angus
You can link with a one-exabyte library if you want but, if you only bring in one object file from it, and that doesn't need any others, you could end up with a very small executable.
In fact, if you link with it but there are no unresolved symbols that it can satisfy, it will have zero effect on your executable size.
That's the way libraries work. They're generally only used to satisfy unresolved externals and usually only bring in the single object file that will satisfy that reference (keeping in mind that doing so may generate more unresolved externals which need to be satisfied).
It's pretty hard to estimate without actually doing the linking since, until you do that, you can't easily tell what will be pulled in from the library.
If you link statically the compiler copys only the needed parts of the lib(s). So the executable can be much smaller than the lib, if you only use small part of the functions.
精彩评论