C Static Libraries : Collection of files

C Static Libraries : Collection of files


A library in C is a collection of files meant to be used by other programs.?

The library consists of two types of files, a header, the interface expressed in a .h file which contains prototypes of functions, and a function or implementation in a compiled *.c file (object file *.o).?

The libraries can be Static or Dynamic (also known as Shared). Static Libraries are linked to the executable file, in this case, an *.exe contains all routines for a program to run. On the other hand, Dynamic libraries are not part of the executable file because they are loaded at runtime, in this case, the *.exe file needs an external link or dynamic loader to access the library routines successfully.

The reason to use libraries is to simplify a code, splitting the source code into small units of related files, that can be managed separately by a unique file. Furthermore, libraries may call functions in other libraries to do various tasks.?

Static Library

A static library consists of one or more object files, these object files that have been compile are turned into a library as an archive by the ar archiver. The program takes files and stores them in a bigger file.?Then the linker copies the library to every program file that invokes the static library to get an executable file.

In Unix and Linux OS, the filename for the library usually starts with "lib" and ends with *.a and the object files with *.o . Microsoft Windows OS use a *.lib extension for libraries and an *.obj extension for object files.

Here is an example of how to create them:

  • Group the *.c files of the library:

No hay texto alternativo para esta imagen

  • Once the files are selected to merge into the library, it's time to create the object files.

gcc -Wall -Werror -Wextra -pedantic -c *.c        
No hay texto alternativo para esta imagen

  • The object files ( *.o) and the header file (*.h) created with the prototypes of each function are ready to pass the archiver. The 'r' flag indicates to replace older object files in the library, with the new object files. The 'c' flag indicates to create the library if it doesn't already exist.

ar -rc libname.a *.o        
No hay texto alternativo para esta imagen

  • After an archive is created or modified, there is a need to index it. In case the archiver doesn't show a file already indexed, use the following command:

ranlib libnambe.a        

Keep in mind that a copy of the library becomes part of every executable that uses it,?to update the library, the entire executable needs to be replaced.

A Static Library must be included in the linking process with the flag -l to use the library in a program, without the prefix lib and sufix .a, as follows:

cc main.o -L. -lname -o programname.exe        


Blog entry to describe static libraries in C and how to create them. Thanks for reading!

要查看或添加评论,请登录

Angela Vergara Salamanca的更多文章

  • How the network delivers information to our devices?

    How the network delivers information to our devices?

    We get used to the idea of typing in a web browser any search, but how to deliver that information to our computers…

  • IoT Basics

    IoT Basics

    The internet of things, widely known as IoT, is a system of unique identified devices interrelated to transfer data…

  • Recursion in a nutshell

    Recursion in a nutshell

    Recursion is a programming technique used in procedures, functions, or algorithms. This technic defines a termination…

  • GCC - How to get an executable file?

    GCC - How to get an executable file?

    In the early 1970s, Dennis Ritchie developed the C programming language as an improvement of B. B by Ken Thompson was…

    1 条评论

社区洞察

其他会员也浏览了