C - Static libraries

C - Static libraries

  • Why use static libraries

Because libraries are a files containing several object files, that can be used as a single entity in a linking phase of a program, that′s mean only the program's executable file is needed in order to run the program.

  • How static libraries work

Static libraries are just collections of object files that are linked into the program during the linking phase of compilation.

  • How to create static libraries

To create a static library is needed to follow the next step:

First, set collection of funtions (C files) that will belong to the library.

No hay texto alternativo para esta imagen



Second, compile with gcc all the files, obtained before, and get the ".o" extension of the ".c" functions using the "-c" flag. This step is to get the assembly code of each ".c" file. (The flags -Wall, -pedantic, -Werror, Wextra will help to convert all warnings in error when the files are compiled)

No hay texto alternativo para esta imagen

Now, the assembly files were created.

No hay texto alternativo para esta imagen




Third, all assembly files (".o extension") will be passed to the library libholbertonschool.a. To do it, the "ar" program (archiver) will be used accompained by the flags -rc. (The 'c' flag tells ar to create the library if it doesn't already exist. The 'r' flag tells it to replace older object files in the library, with the new object files).

No hay texto alternativo para esta imagen

Now the library was created. To see what library has, it will be used the "ar" program with the "-t" flag. (The flag 't' list the files allocated in the library)

No hay texto alternativo para esta imagen

Those are the file stored in "libholberton.a" library.

No hay texto alternativo para esta imagen



Fourth, it will index, with ranlib program, the library to be later used by the compiler to speed up symbol-lookup inside the library.

No hay texto alternativo para esta imagen
  • How to use static libraries

To use a static library is necesary compile the main funtion with the library using "-l" flag. ('l' flag add the library's name to the list of object file names given to the linker).

For example:

First, create a main funtion with the funtion′s prototype.

No hay texto alternativo para esta imagen




Second, compile the main funtion with library using 'l' flag to create alpha file executable using 'o' flag. ('L.' flag tells the linker that libraries might be found in the given directory)

No hay texto alternativo para esta imagen

Third, execute the "alpha" file.

No hay texto alternativo para esta imagen

This will be the output of the previusly executed file.

No hay texto alternativo para esta imagen

REFERENCE

Guy Keren. (1998-2002). Building And Using Static And Shared "C" Libraries: AS IS . Recuperado de https://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html#what_is_a_library.

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

Arturo Victoria Rincon的更多文章

  • Dissecting an Executable and Linkable Format (ELF)

    Dissecting an Executable and Linkable Format (ELF)

    What is Executable and Linkable Format (ELF)? ELF is a file format, like APA file format, for executable files, shared…

  • Signals in Linux

    Signals in Linux

    What is a signal and why do they exist? A signal is an asynchronous notification sent to a process or to a specific…

    2 条评论
  • Hack the VM

    Hack the VM

    There is an easy way to change information from the memory heap when a program is running and in this blog, we will…

    2 条评论
  • Discover EmailScrapper and contact job seekers through a bot

    Discover EmailScrapper and contact job seekers through a bot

    Introduction Email Scrapper Service helps talent recruiters to contact applicants to jobs offered in the "Un Mejor…

  • What happens when you type holbertonschool.com in your browser and press Enter

    What happens when you type holbertonschool.com in your browser and press Enter

    Introduction In the last 20 years the digital world has revolutionized the planet to bring closer the information to…

  • Internet of Things, a world online

    Internet of Things, a world online

    Introduction Nowadays, the "information" is the main topic in the world due to the knowledge of what's happening…

  • Machine Learning, an approach to human intelligence

    Machine Learning, an approach to human intelligence

    When a human baby is born, his brain begins to develop its internal networks. Thanks to that, the baby still can't…

  • Python3: Mutable, Immutable... everything is object!

    Python3: Mutable, Immutable... everything is object!

    What is an object? An object in programming is an abstract data type. It can include multiple properties and methods…

    2 条评论
  • Class and instance attributes

    Class and instance attributes

    What's a OOP (Object-oriented programming) Object-oriented programming (OOP) is a programming language model that…

  • Unix system libraries

    Unix system libraries

    Why use libraries A library is a file containing several object files, that can be used as a single entity in a linking…

社区洞察

其他会员也浏览了