Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 466
sashar Avatar asked Dec 04 '25 17:12

sashar


1 Answers

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

like image 100
twalberg Avatar answered Dec 06 '25 09:12

twalberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!