Libraries

Libraries represent a collection of compiled objects in a single file.

Their advantages are: reusability of components (using one shared library takes up less disk space), version management (old and new versions coexist simultaneously on one Linux system), component specialization (developers can focus their core competency in one library).

The types of libraries are: static (object code in a linked library, which becomes part of the application) and dynamic (shared objects, dynamically linked at runtime).

Structure of a library

The figure below shows a sample structure of a library:

08_library.png

Directory Structure

Use the following commands to create the directory structure:

Reference:Creating a shared and static library with the gnu compiler gcc

Library files

In the folder libexample/src/addlib/, create a file add.c with the following content:

In the folder libexample/src/addlib/, create a file add.h with the following content:

In the folder libexample/src/addlib/, create a file answer.c with the following content:

In the folder libexample/src/addlib/, create a file answer.h with the following content:

In the folder libexample/src/, create a file main.c with the following content:

Last updated

Was this helpful?