Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we have two kinds of libraries in C/C++?

Tags:

c

There are static libraries and then there are shared libraries. Wouldn't it be possible to have just the shared ones and link them in statically if needed?

Compiling once with -fPIC and once without seems like a waste. I don't know much assembly, but shouldn't it be possible to transform rellocatable code into static code orders of magnitude faster than recompiling everything?

like image 434
PSkocik Avatar asked Jan 05 '16 22:01

PSkocik


People also ask

What is the purpose of libraries in C?

The C standard library provides macros, type definitions and functions for tasks such as string handling, mathematical computations, input/output processing, memory management, and several other operating system services.

What is the difference between shared 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 and dynamic library in C?

A static library must be linked into the final executable; it becomes part of the executable and follows it wherever it goes. A dynamic library is loaded every time the executable is executed and remains separate from the executable as a DLL file.

How many libraries does C have?

The ANSI C standard library consists of 24 C header files which can be included into a programmer's project with a single directive. Each header file contains one or more function declarations, data type definitions and macros.


2 Answers

This is partially a historical issue. Once there were only static libraries. They were linked statically to every binary the system compiled. However this represented a maintenance nightmare among other things, requiring all the using packages to be recompiled if a library was patched or changed.

Then shared libraries came along fixing these issues. Now for your question, firstly there are some significant optimisations that can take place in a statically linked library that it is impossible to perform on a dynamic one, therefore even if one were to transform dynamic libraries into static ones it would likely be less efficient than code compiled statically in the first place.

Secondly, most modern systems use solely shared libraries anyway, so there is not much of an issue, things are compiled only once, as a shared library.

As a slight aside, however still relevant you might look into prelinking. A step that takes away some of the start-up overhead (though still not necessarily achieving the same performance as a static link) and allowing software that dynamically links in libraries to launch faster.

like image 122
Vality Avatar answered Oct 08 '22 14:10

Vality


Although in theory, one surely could post-process dynamic libraries into static ones, the difficulty of such a task -- especially of doing it well -- would be comparable to compiling from scratch. Getting the same result by post-processing as by performing a from-scratch static build is probably more difficult than simply building again, plus any tool to do that would carry its own maintenance burden. Why go for that when there is already a perfectly good way to reach the same objective?

Moreover, building both static and shared libraries is by no means required. Even where you want to do that, the incremental cost of doing so is (should be) a quite small part of the overall development time.

like image 23
John Bollinger Avatar answered Oct 08 '22 14:10

John Bollinger