开发者

Static library & Dynamic library : Confusion

I need little clarification in this area. I feel that the terms Static library & Dynamic Library are not correct.

  • lib1.o + lib2.o + lib3.o --> "LinkerOutputFile"(executable or library).
  • If this "LinkerOutputFile" contains the code of all the files lib1.o , lib2.o, lib3.o then its said that "LinkerOutputFile" is satically-linked "LinkerOutputFile"(executable or library). (or)

  • If "LinkerOutputFile" just contains references & other information about to lib1.o, lib2.o, lib3.o without containing the code of these lib*.o files. Then its said that "LinkerOutputFile" Dynamically linked.

How does this make lib*.o files either a static library or dynamic library? They are just library files.

Or is it that, "LinkerOutputFile" is a library instead of executable then depending on whether its statically-linked or dynamically-linked its called static-library or dynamic-library. True or False?

I know I'm wrong, because, I know that

On most unix-style platforms the extensions are

  • .a for static libraries (archives) and
  • .so for shared libraries.

On Windows:

  • .dll indicates a shared library
  • .lib indicates a static or import library.

But can't figure out where I'm wrong. Also please tell me how are Static library & Dynamic Library internally different.

Also, this is from ABI specification:

This chapter describes the object file format, called ELF (Executable and Linking Format). There are three main types of object files.

A relocatable file holds code and data suitable for linking with other object files to create an executable or a shared object file.

An executable file holds a program suitable for execution; the file specifies how exec(BA_OS) creates a program's process image.

A shared object file holds code and data suitable for linking in two contexts. First, the link editor [see ld(BA_OS)] processes the shared object file with other relocatable and shared object files to create another object file. Second, the dynamic linker combines it with an executable file and other shared objects to create a process image.

"shared object file" are other words for saying Dynamic Library(has *.so extension). But what about "Static library"? This doesn't even mention it.

Is there any difference between, "Relocatable" & "Static Library" & "Import Library"开发者_运维技巧? Please clarify me with internal structure.


.o files are not any kind of library file. They are an object file.

.a/.lib files are linked at build time. They cannot be replaced after the fact. This makes them static.

.so/.dll files are linked at runtime. They can be replaced any time before execution starts. This makes them dynamic.

Relocation refers to placement of a binary in memory; code from static libraries is integrated with the binary and so can't be relocated independently.


Static libraries contain code that is copied into the executable. Code in the library that isn't referenced by your program is removed. A program with only static libraries doesn't have any dependencies during runtime.

Dynamic libraries are linked during runtime -- a program with references to a dynamic library will load and link with the library when it starts up (or on demand).

A Relocatable library is another word for a dynamic library. When you link with a dynamic library, the addresses of the functions contained within are computed, based on where the library is loaded in memory. They are "relocatable" because the addresses of the contained functions are not determined at link time. (In a static library, the addresses are computed during link time.)

An object file (.o) contains compiled code, but doesn't contain the final addresses of all of the functions. Linking is the process where the linker goes through all of the object files and computes the correct address for each function that is called.


Let's say I have my App that links with StaticLib.lib, then all the code from both my App and StaticLib.lib will be in the same executable. This means that StaticLib.h contains functions implementation in it.

Now if I link App with DynamicLib.lib, then my App will use functions implemented in DynamicLib.dll that means that DynamicLib.lib constains references, something like:

  • OpenWAV is found at offset 1 in DynamicLib.dll
  • CloseWAV is found at offset 2 in DynamicLib.dll etc

The name dynamic means that it can be loaded by any App, not really by linking with DynamicLib.lib, but calling LoadLibrary () and importing the functions exported by the DLL by hand.

About Relocatable, well at this moment I'm not familiar with the word.

I'm talking as a Windows programmer so I don't what really is going on on Linux, but judging from the name SO(Shared Objects) are same thing like DLLs.

Hope my answer was helpful!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜