C Static Libraries
Image taken from https://miro.medium.com/max/958/1*y3_jHBbJ-kwwfJIhs2s_4g.jpeg

C Static Libraries

What is a library?

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

Why use C Libraries?

We use C libraries because they allow faster linking done by the compiler because all the object files are together in one file, as opposed to linking a program whose object files are separate on the disk. And since you don't have to open so many files but rather a single one, it further increases the linking speed.

How they work

Static libraries are collections of object files that are linked into the program during the linking phase of compilation, and are not relevant during runtime. Due to the fact that they are only active during the linking phase, only the executable file of the program is required to run the program.

How to create them

In order to create the library you need to have all your object files ready and preferrably in the same directory. If you already have the object files in your directory then you can just run the "ar" command. However I'm going to describe the step by step process of creatig the library starting with just the files in .c format. If you want to create a local (static C library) you have to follow these steps: 

  1. Move the program files into the directory you are working on (this makes translation and creation of library a lot easier)
  2. Run the compiler with the option to stop at object files (-C) including all your .c files.
  3. Once your files are translated to object files (.o extension) you have to run the archiver command "ar" with the options -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.) this is just to make sure your library is created or updated accordingly. The syntax of the command would be as follows:

ar -rc libyourlibraryname.a *.o

  1. After you've run the archiver command your library will be created. However, if you want your linking to be even faster, indexing is recommended. to index your file all you have to do is run the "ranlib" command followed by your library name. Syntax shown below: 

ranlib libyourlibraryname.a

  • Note: if at anytime you update your library by running step 3 again, re indexing would be advisable as if prevents your compiler having issues by saying that the index is not up to date

After following the above steps then your library is ready to be used!

How to use them

In order to use the library we just created in a program we have to add the name of the library to the list of object file names given to the linker, using a special flag, normally '-l'. Here is an example:

  • gcc main.c -L. -lyourlibraryname -o program

This will create a program using object file "main.c", and any symbols it requires from the "yourlibraryname" static library. Note that we omitted the "lib" prefix and the ".a" suffix when mentioning the library on the link command. The linker attaches these parts back to the name of the library to create a name of a file to look for. Note also the usage of the '-L' flag - this flag tells the linker that libraries might be found in the given directory ('.', refering to the current directory), in addition to the standard locations where the compiler looks for system libraries.


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.

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

Juan Felipe Buitrago Vargas的更多文章

  • My Experience With My Final Project.

    My Experience With My Final Project.

    I want to share with all of you my experience the project that we made for the Holberton School's foundation track…

  • Postmortem

    Postmortem

    Issue Summary From 11:30 PM May 16th to 2:30 AM May 17th EST, our internal API server was down on one of our replicas…

  • What Happens When You Type google.com Or Any Other URL In Your Browser And Press Enter

    What Happens When You Type google.com Or Any Other URL In Your Browser And Press Enter

    Introduction Nowadays in the digital era, we use the internet for just about everything. We can study, connect with our…

    8 条评论
  • Machine Learning in a Breeze!

    Machine Learning in a Breeze!

    Dear reader, our world is constantly changing as our understanding of everything around us gets better and better. A…

  • Python Everything is Object

    Python Everything is Object

    An object is an abstract data type. It has many different properties, attributes, methods and in some cases even other…

  • Dynamic vs Static Libraries

    Dynamic vs Static Libraries

    What is a library? A library is a file containing several object files, that can be used as a single entity in a…

  • What happens when you type ls *.c

    What happens when you type ls *.c

    The UNIX operating system has many built in commands that help us do a vast amount of things with in the terminal. The…

  • What happens when you type gcc main.c

    What happens when you type gcc main.c

    GCC is a shell command that we use to compile a C language source code file. The most basic syntax for this command is…

社区洞察

其他会员也浏览了