> For the complete documentation index, see [llms.txt](https://dimitar-minchev.gitbook.io/linux-system-programming/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dimitar-minchev.gitbook.io/linux-system-programming/02_programming/08_library.md).

# Библиотеки

Библиотеките представляват множество компилирани обекти в един файл.

Техните преимущества са: **повторно използване на компоненти** (използване на една споделена библиотека заема по-малко място на диска), **управление на версиите** (стари и нови версии съжителстват едновременно на една Linux система), **компонентна специализация** (разработчиците могат да фокусират основната си компетентност в една библиотека).

Видовете библиотеки са: **статични** (обектен код в свързана библиотека, който става част от приложението) и **динамични** (споделени обекти, динамично свързвани по време на изпълнението).

## Структура на библиотека

На фигурата по-долу е дадена примерна структура на библиотека:

![08\_library.png](/files/-M50WRe8HPOmZCpJy2IT)

## Структура на директориите

Използвайте следните команди да създадете структурата на директориите:

```
mkdir libexample
mkdir libexample/src
mkdir libexample/src/addlib
mkdir libexample/bin
mkdir libexample/bin/static
mkdir libexample/bin/shared
```

Източник: [Creating a shared and static library with the gnu compiler gcc](https://renenyffenegger.ch/notes/development/languages/C-C-plus-plus/GCC/create-libraries/index)

## Файлове на библиотеката

В папка `libexample/src/addlib/` създайте файл `add.c` със следното съдържание:

```c
#include <stdio.h>
int gSummand;
void setSummand(int summand) {
	gSummand = summand;
}
int add(int summand) {
	return gSummand + summand;
}
void __attribute__ ((constructor)) initLibrary(void) {
	printf("Library is initialized\n"); 
	gSummand = 0;
}
void __attribute__ ((destructor)) cleanUpLibrary(void) {
	printf("Library is exited\n"); 
}
```

В папка `libexample/src/addlib/` създайте файл `add.h` със следното съдържание:

```c
void setSummand(int summand);
int add(int summand);
```

В папка `libexample/src/addlib/` създайте файл `answer.c` със следното съдържание:

```c
#include "add.h"
int answer() {
	setSummand(20);
	return add(22); // 42 = 20 + 22
}
```

В папка `libexample/src/addlib/` създайте файл `answer.h` със следното съдържание:

```c
int answer();
```

В папка `libexample/src/` създайте файл `main.c` със следното съдържание:

```c
#include <stdio.h>
#include "addlib/add.h"
#include "addlib/answer.h"
int main(int argc, char* argv[])
{
	setSummand(5);
	printf("5 + 7 = %d\n", add(7));
	printf("And the answer is: %d\n", answer());
	return 0;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dimitar-minchev.gitbook.io/linux-system-programming/02_programming/08_library.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
