Hi friends, here I have several problem for static libraries need your help.
From some books I learned that, a static library (.a in Linux) contains a set of compiled objects, when it's linked into an executable, the link tool will only take out those objects that are actually referenced.
So if the .a contains 1.o, 2.o and 3.o and my application only uses functions in 1.o, then only 1.o will be built into the executable, is this correct?
Then let's go further, say we have 2 .a libs, first contains 1.o, 2.o and 3.o and second contains 3.o 4.o and 5.o. If my application only uses functions in 1.o 2.o 3.o and 4.o then only these these 4 .o will be built into the executable, is it correct?
I raised this question because I'm building some .a files to use with MSVC. These .a libraries are built in MinGW and then should be compatible with MSVC. I could include these libs into the MSVC project and build my program successfully. But the generated executable is 5 MB (the total size of all .a should be about 8MB) even when my program is an empty one (only have an empty main function).
Does this mean, .a when used in MSVC or .lib (static libs for Win) will be built into executable as a whole, but not in the way it behaves under Linux?
1. Not necessarily. It depends on whether the linker can do that.
2. You have a conflict on 3.o, but assuming that point 1 is true, and the conflict is resolve, then yes.
I'm building some .a files to use with MSVC
You can't, or more correctly, you shouldn't try. You have to build them with MSVC which generates a .lib.
You library isn't the only thing in a program. Even when you specify nothing, there's the C runtime., or the even larger C++ runtime and all the statics amd crap that gets shoved in by default.
As for the 5MB executable, I admit that there may be other things involved. But, an empty project definitely generate a quite small exe (without libs). So I guess size bloat is related to the .a (In fact I'm building with ffmpeg static libraries built under MinGW, they only come as .a files. So maybe can't get .libs).