Sunday, October 13, 2019

C Programming Static Library

Static Library 

What is static library?

In the C programming language, a static library is a compiled object file containing all symbols required by the main program to operate as opposed to having to pull in separate entities. Static libraries are either merged with other static libraries and object files during building/linking to form a single executable or loaded at run-time into the address space of their corresponding executable at a static memory offset determined at compile-time/link-time.

Why should we use a static library?

There are several advantages to statically linking libraries with an executable instead of dynamically linking them. The most significant advantage is that the application can be certain that all its libraries are present and that they are the correct version. This avoids dependency problems. Static linking can also allow the application to be contained in a single executable file, simplifying distribution and installation.

With static linking, it is enough to include those parts of the library that are directly and indirectly referenced by the target executable or target library. With dynamic libraries, the entire library is loaded, as it is not known in advance which functions will be invoked by applications depending on the structure of the library.

How to create and use a static library

In order to create a static library first we need to access a terminal or command prompt(Terminal is used in MacOS operating systems while the command prompt is used by Windows Operating systems). After we access the command prompt or Terminal we should create some files written in the C programming language.

We can either use the text editors Emacs, Vim, or Vi, but in this case we will use Emacs.






After we created out files we will compile them using the (GCC) command on the command prompt. 
The GNU Compiler Collection or GCC) is a compiler system produced by the GNU Project supporting various programming languages. For more information on how to use gcc, type the command (man gcc) in your command prompt or terminal.






When we use the gcc command we used the option "-c" accompanied by a wild card "*.c". We use this option when we what to compile files but not link them and the wild card to make sure all files that end with the extension .c end up being compiled. This will create object files or files with the extension ".o".

Next step is  to use the "ar"command. The ar command creates, modifies, and extracts from archives. An archive is a single file holding a collection of other files in a structure that makes it possible to retrieve the original individual files.






The "ar" command with the option -r will insert the files member into the archive and the option -c will create the archive, in this case we will call it "libholberton" with the extension ".a" for an archive. And finally using the wildcard  "*.o" to select all object files. 

Whenever files are added to a library, including the initial creation of the library , the library needs to be indexed, which is done with the command "ranlib". The ranlib makes a header in the library with the symbols of the object file contents.This helps the compiler to quickly reference symbols. A large library may have thousands of symbols meaning an index can significantly speed up finding references. For more information use the "man ranlib" command.





Next, if we want to see the contents of our archive we use the "ar" command the with option "-t". 




















We have now created a static library "libholberton.a" and now let us use the static library by invoking it as part of the compilation and linking process when creating an executable program.





Our "main.c" file contains a quote from Kevin Mitnick, and we will include this file when we compile our program with the "libholberton.a" library. finally we use the option "-L" that will specifies the path to the library. The executable file should be named quote.







Making a bash script to create a static library

Now that we have created out library, how about we do all the above in a single command. We will use a Bash script to combine all previous steps to a single command. A  Bash script is a plain text file which contains a series of commands.

First step is to create a text file that we will call "create_static_lib.sh" using the Emacs text editor. 






After that we add the "#!/bin/bash" command at the very beginning of our text file. Then we proceed to a add each previous command to create a static library one after the other on a new line like this: 

  1. The command "gcc -c *.c" will compile all file with extension ".c" into object files.
  2. The command "ar -rc liball.a *.o" will create our new library with all object files inside.
  3. The command "ranlib liball.a" will index our library.
Finally make sure that you make the file executable by using the command "chmod u+x create_static_lib.sh" so that all users in your computer can run the program. Type "man chmod" in your command prompt or terminal for more information.

That's it! we have successfully created a program that will create a static library.