Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of a library and the executable

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 executable (*.exe) is 29KB, less than the library. What i want to know is :

  1. Since the library is statically linked, I was thinking it should add directly to the executable size and the final exe size should be more than that? Does windows exe format also do some compression of the binary data?

  2. How is it for linux systems, that is how do sizes of library on linux (*.a/*.la file) relate with size of linux executable (*.out) ?

-AD

like image 837
goldenmean Avatar asked Dec 16 '08 05:12

goldenmean


People also ask

What is the difference between a library and an executable?

Executable file will have to be recompiled if any changes were applied to external files. In shared libraries, no need to recompile the executable. Takes longer to execute, because loading into the memory happens every time while executing. It is faster because shared library code is already in the memory.

What is a library executable?

Executable is a Load file which executes directly in system as a program. As per your question, "ls" is a executable which is used to list the current directory contents. The load for "ls" is placed in "/bin" or you can check using command "which ls".

Is a library an executable program?

If the library is linked statically into the program, that means that when the program is built, the result is an executable file that includes both the result of compiling program's source code (the main function and any other function in the program), and the functions from the library such as printf (which the ...

What is the difference between C executable and C library?

A library is exactly like an executable, except instead of running directly, the library functions are invoked with parameters from your executable. You would be familiar about the compilation of a C file.


2 Answers

A static library on both Windows and Unix is a collection of .obj/.o files. The linker looks at each of these object files and determines if it is needed for the program to link. If it isn't needed, then the object file won't get included in the final executable. This can lead to executables that are smaller then the library.

EDIT: As MSalters points out, on Windows the VC++ compiler now supports generating object files that enable function-level linking, e.g., see here. In fact, edit-and-continue requires this, since the edit-and-continue needs to be able to replace the smallest possible part of the executable.

like image 180
David Norman Avatar answered Oct 20 '22 09:10

David Norman


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

like image 34
Greg Hewgill Avatar answered Oct 20 '22 11:10

Greg Hewgill