Static Libraries in C

Static Libraries in CMia MortonBlockedUnblockFollowFollowingMar 3Why use libraries in C?Libraries in C are not unlike public libraries in cities, towns, or neighborhoods.

A public library provides access to a multitude of information in various media forms to the public for access and use.

Functions in a C library can be used and accessed by programmers to create several different programs.

As a programmer, you may find yourself using the same function or functions repeatedly.

In this case, it is best to put this function or functions in a library to speed up the compilation of the program.

C libraries store files in object code; during the linking phase of the compilation process ( Compilation Process) files in object code are accessed and used.

It is faster to link a function from a C library than to link object files from a separate memory sticks or discs.

Two types of libraries in C: Static and dynamicThere are two types of libraries in C static and dynamic.

Dynamic libraries are shared libraries with specific functions launched during the execution of a program and contribute to “reduced memory consumption”(techopedia.

com).

Dynamic libraries are linked in two stages.

Static libraries produce object files and standalone executable files (wikipedia.

org).

These libraries can be linked to a program without recompiling the code.

Static linking or the linking of static libraries creates larger files because of the creation of the standalone (executable file) files.

How do static libraries work?Compilation ProcessStatic libraries are added during the linker phase of the compilation process (above).

During the linker phase: the linker links access all the libraries to link functions to the program.

Static libraries are merged with other static libraries to create an executable program.

During the compilation of a program, the static library is called to execute the program.

How to create static librariesN.

WhiteheadTo create a static library you have to use the ‘ar’ or archiver program.

The idea is that these functions are being archived until needed.

A function saved in .

c format is recompiled into an object file, .

o .

Creating a Static Library step by step:Step 1.

Create all your source files.

Source files hold any functions you will use.

Step 2.

Compile the source files into object files.

Using GCC use this command:$ gcc -c *.

cThis will change the object files to source files.

Step 3.

Create a static library.

Using “libholberton” as an example of a library name, this command creates a Static library.

$ ar -rc libholberton.

a *.

oThe ‘ar’ is the program being used to archive the files.

The ‘c’ tells the program to create a library.

The ‘r’ tells the program the replace or update older files in the library.

With step three a static library has been created.

If needed use the ‘ranlib <libraryname.

a> to index the library.

$ ranlib libholberton.

aIndexing a library will let the compiler know just how old a library file is.

Indexing also gives the library file a header and makes it easier for the compiler to reference the symbols.

This step may or may not be necessary depending on your computer system.

To view the contents of the static library access it using: ‘ar -t libholberton.

a’ in the command line.

The ‘nm’ command will let you see the symbols in your library; nm lib_test.

a.

To use the static library call on the library during the linking phase of program compilation.

gcc main.

c -L.

-lholberton -o main‘-L’ specifies the library path.

 ‘-l’ goes before the library name.

You are able to run the executable program.

$.

/mainHow are static libraries used?We have created a static library and can now use it.

Step 1.

Write a program:#include "holberton.

h"int main(void){ _puts(""My Static Library is working.

""); return (0);}In the header: “holberton.

h” contains function and function definitions used to tell the compiler how to call functionality.

It contains “data types and constants used with the libraries”(geekforgeeks).

When we compile the file we call the library:$ gcc main.

c -L.

-lholberton -o quoteNow we can run the executable:$ .

/quoteIf the library is working properly the output should be:"My Static Library is working.

"We have created a functioning static library.

I hope this article helped you become more than familiar with static libraries and C libraries in general.

They are very useful tools in programming in C and C++.

Since the beginning of C programming, there was a need for libraries, a place to store the commonly used functions in C for easy access and use by programmers using C.

Creating a library tailored to your personal programming needs may save you a lot of time during compilation and programming.

We have discussed how static libraries work, and in encourage you to investigate how dynamic libraries may be of use to you as well.

.. More details

Leave a Reply