C static libraries
https://laptrinhx.com/using-nix-to-fetch-c-libraries-2188377890/

C static libraries

This article is about exploring in C what are the static libraries, why should we use them, how they work, how can be created, and finally how to use them.

Before starting, take a brief look at the compilation phases in c.

  1. Pre - Processing.
  2. Compilation.
  3. Assembly.
  4. Linking.

Libraries are used in the linking phase, which means that at the end are expected a set of ".o" files to be linked on.

What are the libraries in C?

A library is a file that contains several object files and can be used in the linking phase of a program. What we could expect to find when these are used is that the library file is indexed. This makes it easy to find symbols such as functions, variables, and so on, inside of them. which means that the libraries themselves are a collection of code routines that can be called at the moment of the building stage of our programs.

Unix systems allow us to create two kinds of libraries. Static and dynamic (shared).

Why should we use libraries?

There are basically two reasons why is recommended to use libraries.

  • First. Linking a program whose object files are ordered in libraries is much faster than linking a program whose object files are separate on the disk (even in the same dir). Also, we got further speeds up linking because basically, we have just one file to search or at least much fewer files, which helps us to get even further speeds uplinking.
  • Second. these can be called upon when building the program. Which let us use an optimized collection of code routines (block of codes) created for someone else. and guarantee code reusability speeding up the development time.

What is a static library and how does it work?

It is a file containing a collection of object files (*.o). Files that are linked into the program in the linking phase (4) and not are important in the runtime.

No hay texto alternativo para esta imagen


The linker, in this case, copy the code of the library to our object file. So the library becomes a collection of object files merged by the linker. These libraries are normally recognized because have the extensions ".a" or ".lib".

How to create static libraries in c?

  1. Create the ".o" files expected to be passed at the linked phase.

No hay texto alternativo para esta imagen

Just suppose that we have a set of ".c" files in our directory like in the image above. what the linker does expect is to get the ".o" files, then we must make them.

To achieve this, we must use gcc -c, and for speed up use *.c which means a match with all the files ending with ".c" extension in the current dir, otherwise we have to add the files one by one or at least copy in this way.

No hay texto alternativo para esta imagen

2. bundle all object files into one static library.

To create a static library or to add additional object files to an existing static library, we have to use the GNU?ar?(archiver) program.

$ar -rc libr.a *.o        

In this way, we create a static library named "libr.a" and puts copies of the object files ".o" created before in the current dir into the file.

  • The 'c' flag tells ar to create the library if it doesn't already exist.
  • The 'r' flag tells it to insert object files or replace existing object files in the library, with the new object files.
  • *.o. It is again a match, in this case, is about to include all the files that end with ".o" extension.

3. After an archive is created or modified, there is a need to index it. and this can be complete by using the next commands

$ranlib libname.a        

Or directly using a third flag "-s"

$ranlib -rcs libname.a *.o        
No hay texto alternativo para esta imagen

Like we can see this creates the "libr.a" file already indexed.

How do I verify the library content?

In order to list the names of the object files in our library, we can use the ar command with?-t?flag.

ar -t libr.a        
No hay texto alternativo para esta imagen

How to use the static library?

4. Once we have the library file created, the final step is to create the final executable program.

$gcc main.c -L. -lname -o main        

-L: Specifies the path to the given libraries ('.' referring to the current directory)

-l: Specifies the library name without the "lib" prefix and the ".a" suffix, because the linker attaches these parts back to the name of the library to create a name of a file to look for.

Next, we have an example of the right execution of the compilation including the library, in this case using a created function called _isdigit() which returns 1 if the input char is a number or 0 if not.

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

With this, we finish with an executable file, with all the functions listed before, by using just a couple of commands in the shell. Hopefully, you found this useful.

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

Juan Sebastian Gonzalez的更多文章

  • Recursion

    Recursion

    What is it? Essentially is a technique that split a situation or a problem into shorter pieces of itself. Take a look…

  • Mutable, Immutable... everything is an object! O.o

    Mutable, Immutable... everything is an object! O.o

    In Python, everything is an object, which means every entity has some metadata (called attributes) and associated…

  • Differences between static and dynamic libraries

    Differences between static and dynamic libraries

    A library is a powerful tool in a programmer's arsenal, consisting of pre-compiled functions that can be easily reused…

  • How integers are stored in memory using two’s complement

    How integers are stored in memory using two’s complement

    A computer is an amalgamation of hardware and software, where digital memories store information in bits (short for…

  • The worst abuse of the C preprocessor (IOCCC winner, 1986)

    The worst abuse of the C preprocessor (IOCCC winner, 1986)

    obfuscation is the deliberate act of creating machine code that is difficult for humans to understand. This article is…

  • Soft Link Vs. Hard link

    Soft Link Vs. Hard link

    Understanding the difference between a hard link and a symbolic link requires knowledge of how computers store and…

  • Compilation Process using in programs like C.

    Compilation Process using in programs like C.

    In programming, there are two types of languages, which are interpreted and compilated. The code of compiled language…

社区洞察

其他会员也浏览了