WHY USE LIBRARIES?

WHY USE LIBRARIES?

If we think for a moment about a library, we could conclude that libraries store information, but this would only be the tip of the iceberth, since they have the ability to organize, list and maintain functions and declarations in one place to their simultaneous use, thus helping programmers and programs to use them more efficiently.

When we program the libraries, they make our work easier and less repetitive, since it involves those functions that we use on a recurring basis. Libraries come in two static and dynamic types, but for now we will focus on static ones.

STATIC LIBRARIES.

These libraries are made up of object files or ".o", or so to speak files that have not passed through the linker, that is, they are files that are not yet executable, object files are an essential part of the linking process when compiling a program, but they are not part of their execution time,

The efficiency of libraries lies in that by organizing the object files necessary for linking a program and keeping them indexed in one place, it avoids that our machine has to do searches in different parts of the memory, translating this into a saving of time by part of the compiler. Now we think that libraries are perfect for their efficiency, but all this work translates into a cost in exchange since the files resulting from the compilation, that is, the executables, are usually quite large, in addition to updating the library, it must later be recompiled. the program for the update to take effect on it.

how to create a library

To create a library, use a command called "ar" which creates a program with extension ".a".

The first step in the process is to have the ".c" files located in the directory where the ".a" file is going to be created. To copy the files from one place to another, it is recommended to do so with the command " cp -p "since this makes our compiler think that the files to organize are up to date, then we go on to create the object files, the command used for this purpose is" gcc "followed by a flag that is used to create these files which is "-c" represented in our work environment like this:

$ gcc -c *.c

here what we did is create our object files telling gcc to take all files ending in ".c", and convert them to ".o" files.

No hay texto alternativo para esta imagen
No hay texto alternativo para esta imagen

followed by this we use these object files to create our library using the command "ar" accompanied by its flag "-rc", the "c" flag tells "ar" to create the library if it has not been created yet, and the "r" flag tells "ar" to replace the old library files with new ones. To create the library the syntax of this command is like this:

$ ar -rc libfile.a * .o

for this example we will do it with a library that we will call "libholbertonschool.a".

No hay texto alternativo para esta imagen

Now that we have created our library, it is time to use it.

To see the contents of our library we use the command "ar -t" as follows;

$ ar -t libholbertonschool.a

No hay texto alternativo para esta imagen

In case our library is not indexed we can use the "ranlib" command to do it, this command creates an index of the contents of a file and adds it to it.

in this case it would be as follows:

$ ranlib libholbertonschool.a

To use the library we have created a file main.c that will help us to link a program that prints a predetermined phrase and in an ordered way.

No hay texto alternativo para esta imagen

when we try to compile the main.c file we have a message like the one in the following image and it stops the process completely

No hay texto alternativo para esta imagen

so how to make it work? it is simple what we do is link our main.c file with our library, and voila. and we do this in the following way:

$  gcc main.c -L. -lholbertonschool -o phrase

In the previous command we link the main.c file with the holbertonschool library, but if we look at the name of the library it does not have the prefix "lib" and neither does the ".a instead it has -l that specifies the library and has the flag "-L" that specifies the address, also after the library we have put "-o" that helps us to establish the name of the executable file where all the process we perform will end and in this chaos is the file "phrase" .

No hay texto alternativo para esta imagen

Then we use "./" to execute the phrase file and voila we have an executable program that we link to with a library.

$ ./phrase

No hay texto alternativo para esta imagen


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

Didier Revelo的更多文章

  • ?Quién es el impostor?

    ?Quién es el impostor?

    ?Quién es el impostor?, huuum, aunque suena a título de videojuego o a película de espías, esta es la pregunta más…

    1 条评论
  • LO BUENO, LO MALO Y LO FEO DE QUERER SER PROGRAMADOR

    LO BUENO, LO MALO Y LO FEO DE QUERER SER PROGRAMADOR

    Primero dire que todo lo contenido en esta publicación es mi punto de vista y no debería afectar la decisión de…

    4 条评论
  • ONE STEP FORWARD, ONE STEP TO THE FUTURE.

    ONE STEP FORWARD, ONE STEP TO THE FUTURE.

    Our project sought the connection and consumption of the API Stripe, with the banking platform called Bankity. our team…

    4 条评论
  • WHY USE LIBRARIES? RELOADED.

    WHY USE LIBRARIES? RELOADED.

    Libraries. As we create programs, we find that some pieces of code are repeated over and over again in our work and we…

  • What happens when you type gcc main.c?

    What happens when you type gcc main.c?

    Have you ever wondered what happens when you create a C-based code and compile it with GCC? very well if you asked…

  • What is the difference between a hard link and a symbolic link?

    What is the difference between a hard link and a symbolic link?

    If you are starting in the fascinating world of programming, and you have come across the words "HARD LINK" and…

  • What happens when you type ls *.c

    What happens when you type ls *.c

    When we think of a command we must also think of the environment that performs its function, and for this case, this…

社区洞察

其他会员也浏览了