Static libraries

Static libraries

A library is a file that contains multiple object files, which can be used as a single entity in a linking phase of a program.

No hay texto alternativo para esta imagen


how do they work?

Static libraries are just collections of object files that are linked to the program during the linking phase of compilation and are not relevant at run time. The static libraries are the way in which all the functions that our project may have are grouped, with the purpose of improving compilation, links and improving the order in our project.

No hay texto alternativo para esta imagen


Creation

Steps to consider:

1) have .c files:

it is necessary to have the files that we want to add to the library; example:

main.c
_putchar.c
holberton.h
...        

2) Convert files *.c to *.o:

we do this with the command:

gcc -c *.c        

this command generates the * .o file (Compile and assemble).

This command generate the result of this command is:

main.o
_putchar.o
holberton.o
...        

3) Static library creation

use the command:

ar rc <name-new-library>.a *.o        

With this command, all files of type * .o are added; <name-new-library> is the name of our new library, with extencion .a (example libstatic.a).

4) Index static library

we need the command:

ranlib <name-new-library>.a        

This command is to index the library, it facilitates the reading of the functions.

We want to use it in a program. This is done by adding the library name to the list of object file names given to the linker, usually using a special '-l' flag. Here is an example:

cc main.o -L. -lutyl -o prog        

This will create a program using the object file "main.o", and whatever symbols it requires from the static library "util".











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

Andres Castaneda的更多文章

  • {"error": "forbidden"} datadog postmortem

    {"error": "forbidden"} datadog postmortem

    Issue Summary From 6:00 pm to 7:00 pm GMT-5, after creating monitoring to the server (Create an account, install…

    7 条评论
  • Request and response diagram on the internet

    Request and response diagram on the internet

    In this article we will understand what happens every time we search a web page (for this example we will use…

  • Explaining Internet of Things (IoT)

    Explaining Internet of Things (IoT)

    What is IoT? The Internet of Things (IoT) describes the network of physical objects—“things”—that are embedded with…

  • Compiler GCC in C

    Compiler GCC in C

    My GITHUB - My Twitter GCC was first released March 22, 1987, available by FTP from MIT. Stallman was listed as the…

社区洞察

其他会员也浏览了