开发者

Why the size of exe file is more, when we use a static library than the shared library

开发者 https://www.devze.com 2023-03-19 21:08 出处:网络
I got some question regarding the shared and static libraries.So let me tell you what i did is, I have created a static library and also a shared library using the gcc commands and the size of libarit

I got some question regarding the shared and static libraries.So let me tell you what i did is, I have created a static library and also a shared library using the gcc commands and the size of libarith.a is 6KB and size of libarith.so is 18KB.Both the libraries contain the same functions.

I used one simple function(say add(a,b)) in my make.c file and linked the static library using these gcc command,

           gcc -L/cygdrive/c/cpros make.c -larith.a # used a static library

the size of the exe file is 16.1 KB.

Next I used the same function(same add(a,b)) in my make.c file but these time using shared library, like these.

          gcc make.c /cygdrive/c/cpros/libarith.so #used a shared library

unfortunately the size of exe file is 12.1 KB. I wonder how? becuase the size of libarith.so is 18 KB when compared to libarith.a which is just 6 KB.But at the end the exe file generated开发者_Go百科 using the shared library is less compared to static library.

Can anyone give me a good reason for that ? If shared libraries consume less memory then why dont we go for shared libraries all the time.Please let me know when do we use shared libraries and when do use static libraries.

Yes when I was creating a shared library using -fPIC it gave me a warning position independent code. What does that warning actually mean?


The shared library has a lot of relocation code in it, which is why it is larger than the static library.

When you compile an executable with a static library, the static library is included in the executable. When you compile an executable with a shared library,

There are many reasons why you would choose static over shared libraries and visa-versa. If you are writing code for an embedded platform, you'd choose static. If your library is used by many other applications and you want to upgrade it without having to recompile (within reason) all the applications, then you'd choose shared.

You are also confusing two terms. Memory != Storage. On disk, the shared library may be larger, but when installed you will only have one copy of it. On the other hand, every executable compiled with the static library will have a copy of the static library inside it.


With shared libraries, the code for the library functions is not inserted into your binary. Thus, the binary is smaller. The downside is that you can get problems with version mismatches between applications and the libraries they are using (aka DLL Hell), since the coupling is fairly loose.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号