Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How static libraries work? (C/C++)

I know how to use and create them, but I can't find a text about how it is implemented, how function call happens and so on, can someone help me with that information? Because I want to understand them, but not just know what is it and how it is working

like image 529
Hate Avatar asked Oct 06 '11 15:10

Hate


People also ask

How do static libraries work?

In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.

What is a static library in C?

In the C programming language, a static library is a compiled object file containing all symbols required by the main program to operate (functions, variables etc.) as opposed to having to pull in separate entities. Static libraries aren't loaded by the compiler at run-time; only the executable file need be loaded.

Is C standard library static or dynamic?

It's specific to the version of the compiler used. This library is always statically linked, even when using a dynamically linked UCRT.

What is difference between static and dynamic library in C?

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.


1 Answers

As you may know, when you compile a source file you get an object file. Depending on your platform its extension may be .o or .obj or anything else. A static library is basically a collection of object files, kind of like a .zip file but probably not compressed. The linker, when trying to generate an executable tries to resolve the referenced symbols, i.e. locate in which object file (be it in a library or otherwise) they are defined and links them together. So, a static library may also contain an index of defined symbols in order to facilitate this. Exact implementation depends on the specific linker and library file format but the basic architecture is as mentioned.

You may want to check the italicized keywords in Wikipedia or something for more info on them.

like image 66
cyco130 Avatar answered Sep 22 '22 11:09

cyco130