sashar
sashar

Reputation: 73

size of executables from dynamic/static lib

I was just experimenting with the static and dynamic library stuff. I made two .c files (say file1.c, file2.c) and two .h files which contained some functions defined and declared in them respectively.

I also made a new .c which will be calling the functions in these above two described .c files.

I wanted to check the size of the executable I get if I use a shared library/ static library, so I made a static lib (libstat.a) and a shared lib (libshar.so) from the .o s of the files file1.c and file2.c.

On linking these libs to my main .c I find that the executable size is more in case of the dynamic lib. That is not expected, right? In case of use of shared lib the lib is loaded run time so why does it is having more size?

I am using the following commands:

static lib case

gcc -c file1.c file2.c

ar -cvq libstat.a file1.o file2.o

gcc -o ex1 mainprg.c -L . -lstat

dynamic/shared lib case

gcc -c -fpic file1.c file2.c

gcc -shared file1.o file2.o -o libshar.so

gcc -o ex2 mainprg.c -L . -lshar

I find that libshar.so has more size than libstat.o and ex2 has more than ex1. I expected this results to be reverse. Can somebody explain me the reason?

Upvotes: 3

Views: 1256

Answers (1)

twalberg
twalberg

Reputation: 62369

How much more/less? I wouldn't find it too surprising to see a little more space used for a dynamic binary over static if the stuff in the libraries is trivial. The dynamic binary needs to contain extra symbol information and other metadata to facilitate run-time linking. Much of that metadata can be left out when static linking, so if the functions and/or data in the library are fairly small, that might be what you're seeing. For more substantial libraries, though, with a lot more code and data, or when you're linking with a lot of different libraries, the binaries should be considerably smaller...

Upvotes: 1

Related Questions