开发者

shared and static libraries

开发者 https://www.devze.com 2023-03-15 08:34 出处:网络
Can anyone explain me what is the difference between a shared and static library in gcc(makefile) I read that static library is independent code but it increases the size of your exectuable file

Can anyone explain me what is the difference between a shared and static library in gcc(makefile)

I read that static library is independent code but it increases the size of your exectuable file

But whereas the shared library it links the functions dynamically and it does not increase the size of your executable file

I cannot understand the difference between these two.

Can anyone tell me when i should create a static library and when i should create a shared library.

They say shared library is a position dependent code

What do we mean by position dependent code?

If shared library does not increase the code and if static library increases the code size then we can just go for shared library right.

But why do we have static library too w开发者_StackOverflowhat is the real use of it.

please help me guys


When you compile code with gcc, it can directly produce object files and a number of object files can be linked together to produce an executable file.

A static library is simply a collection of object files (usually with an index) which can be used by a linker when it creates an executable.

The difference between just linking object files together and linking with a static library is that with the library, the linker will only pick the object files from it that are absolutely needed, whereas linking with object files, the linker is forced to take all of the files.

A dynamic library is different again - very different. It is a collection of object files, but this time its the output of a linking process with all of its internal links already resolved.

Linking with a dynamic library means that the linker only resolves symbols in the final executable, but adds none of the object code from the library at link time.

Running an executable linked with dynamic libraries means that at run-time some special software has to ask the operating system to load a specified dynamic library into memory (this is why a dynamic library has to be created by a linker) usually before the program gets to main().

This executable can be a very small file on disk, but when loaded into memory and after loading dynamic libraries the total memory image can be quite large as the whole of the library will have been loaded in.

I believe its the case that if several different programs need the same dynamic library, the operating system can use virtual memory for each program to load the library, but this only uses one copy of physical memory - which can be a big saving in some cases.

If you ship an executable which needs a dynamic library, then you might be lucky and the file might already be on the user's machine. If not, you would have to install that as well. However, if you find bugs in your library, then just shipping a new version of your dynamic library will fix the bugs.

An executable built with static libraries comes ready to run, but fixing bugs would involve reshipping the whole executable.

In Summary:

               object files  static library  dynamic library
produced by    compiler      librarian       linker
exe            large         smaller         smallest
ram x 1 copy   large         smaller         largest
ram x n copies large         smaller         smallest
dep on files   independent   independent     dependant
upgrade lib    no            no              yes


  • you might want to create a single executable without the dependencies of the libraries.
  • as far as I know static libraries should be somewhat faster.

Drawbacks: linking shared libraries is not only increasing the executable size it also gets loaded with each executable. If you use the library in multiple executables then it will be in memory multiple times.

Position dependent code means that at the creation of the library the exact location in memory of the library isn't known so only relative memory addressing is available (eg: jump to +50 bytes from this instruction and follow execution from there)

0

精彩评论

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