Static Libraries in C

Static Libraries in C

What is a C static library?

One of the tools that compilers supply us with are libraries. A library is a file containing several object files, that can be used as a single entity in a linking phase of a program. Normally the library is indexed, so it is easy to find symbols (functions, variables and so on) in them. For this reason, linking a program whose object files are ordered in libraries is faster than linking a program whose object files are separate on the disk. Also, when using a library, we have fewer files to look for and open, which even further speeds up linking.

Why use them?

Why would we ever use libraries if they increase size of our program and make us recompile it every time some changes are made to it? The answer is simple,?have you ever written the same code over and over again in different programs and you wish there was some magical way to just skip that part. Well that’s what libraries do. You can add those pieces of code to the library and just include a library in your compilation process and compiler will insert that code into your program when you want it to, without you having to write it over and over again.

Preparing our files to create a static library

To create a static library, we need to specify to the compiler, which is GCC in our case, that we want to compile all library codes (*.c) into object files (*.o) without linking. To do that we are going to use the command below.

   gcc -c *.c
        


Note that the "*.c" matches all files in the current working directory with the ".c" extension.

Once we have object file(s), we can now bundle all object files into one static library.

How to create a Static Library?

The basic tool used to create static libraries is a program called?'ar', for 'archiver'. This program can be used to create static libraries (which are actually archive files), modify object files in the static library, list the names of object files in the library, and so on. In order to create a static library, we can use a command like this:


  ar -rc libutil.a add.o mul.o util_math.o
          

This command creates a static library named 'libutil.a' and puts copies of the object files "add.o", "mul.o" and "util_math.o" in it. If the library file already exists, it has the object files added to it, or replaced, if they are newer than those inside the library. 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. (we could also use ar -rc libutil.a *.o to add all of them at once)

After an archive is created, or modified, there is a need to index it. This index is later used by the compiler to speed up symbol-lookup inside the library, and to make sure that the order of the symbols in the library won't matter during compilation. The command used to create or update the index is called?'ranlib', and is invoked as follows:


  ranlib libutil.a
          

Well, now that we got here, How do we use them?

After we created our archive, we want to use it in a program. This is done by adding the library's name to the list of object file names given to the linker, using a special flag, normally?'-l'. Here is an example:


  gcc main.o -L. -lutil -o main
           

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.

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

And that's all about static libraries, I hope you find this blog useful. Thanks for reading!

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

Ignacio Peralta的更多文章

  • Recursion

    Recursion

    What is Recursion? Recursion is the process of repeating items in a self-similar way. In programming languages, if a…

  • Static Libraries vs. Dynamic Libraries

    Static Libraries vs. Dynamic Libraries

    Why using libraries? In programming, a library is a collection of pre-compiled pieces of code that can be reused in a…

    1 条评论
  • GCC - Steps of compilation

    GCC - Steps of compilation

    What is GCC? This acronym stands for GNU Compiler Collection, is an integrated distribution of compilers for several…

  • Hard Links and Soft Links

    Hard Links and Soft Links

    The inodes An inode is something that contains the full description of a file or directory, including what it contains…

社区洞察

其他会员也浏览了