Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 597
pecker Avatar asked Feb 11 '10 18:02

pecker


People also ask

What is dynamic and static library?

Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries, on the other hand, exist as separate files outside of the executable file.

What is static library in C++?

Static Libraries. A static library contains object code linked with an end-user application, and then becomes part of that executable. A static library is sometimes called an archive since it is just a package of compiled object files. These libraries are in directories such as /lib, /usr/lib or /usr/local/lib.

What is the point of static libraries?

The benefit of using a static library is that the functions and other symbols loaded into it are indexed. This means instead of having to look for each entity on separate parts of the disk, the program need only reference a single single archived object (. a) file that has ordered the entities together.

Which is better static or dynamic library?

Static: takes longer to execute, because loading into the memory happens every time while executing. Dynamic: faster, because shared library code is already in memory.


1 Answers

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

like image 126
Ignacio Vazquez-Abrams Avatar answered Oct 02 '22 03:10

Ignacio Vazquez-Abrams