Let’s take a closer look at how static libraries work and unravel some of the additional awesome benefits…How Static Libraries WorkIn order to make a program executable, you first need to have a compiler compile it so that the computer can interpret the language that you’ve written it in and package it so that it’s ready to go.
The last part of this compilation process is called the linking phase.
This is where the linker (a software program) combines the program you’ve written with other files (ex: functions) necessary to make it work — this is where your library comes into play.
You can use the gcc command to have all your functions compiled into object code in advance and then put them in your library file.
Later when you use the gcc command to actually compile your program, you can include the library file so that all those functions are able to be read during the linking phase.
Behind the scenes, this allows the linker to link the program faster because the object code is consolidated into one file (the library) as opposed to separate files.
Also, since the library can be indexed, it allows the linker to find the required symbols (functions, variables, etc) faster.
Creating a static libraryNow that we’ve discussed how the library works, here’s how you can create one:Copy and move all the files that you want to be part of your library to the same directory (you could consider creating a new directory for this).
By moving them to the same directory, you’ll be able to quickly convert them to object files.
There are several ways to go about copying and moving files.
The quickest way I’ve found so far is using the cp command to do both in one command.
You can provide the cp command with multiple files you want to be copied and moved.
copying and moving files that I want to add to my library2.
Convert all your c files into object code so that they can be interpreted during the linking phase of compilation.
You can do so by using the gcc command to compile them all the way through the assembly phase where assembly code is converted into machine instructions a.
a object code.
By using the * wildcard in combination with the .
c extension, you can compile all the c files in your current directory at once.
compiling all my files so that they are converted to object codehere we can see that object files were created for each .
Create your library using the ar command.
When doing so, provide the ar command with whatever name you would like to give to the library(it must end with .
a so that it’s recognized as a library) and use * wildcard in combination with the .
o extension to specify that you want all your object files to be included.
You’ll also want to include the -c and -r flags as well.
The former safeguards against creating a library if one with the same name already exists.
The latter allows you to replace older object files with newer versions.
creating my library with all my object files4.
Index the library so that the linker can find symbols faster.
To do so, simply use the ranlib command and provide the name of the library you wish to index.
indexing the library5.
After creating the library, you’ll want to check that all your object files are there.
You can do so by using the ar command with the -t flag.
all the object files appear to be accounted for!6.
You also want to use check that all the necessary symbols from your object files are present in your object files.
You can use the nm command to do so.
It’ll show you the symbol value (where your symbols are located within the files) and symbol type.
I can see the symbol value and type for each symbol in my library!Using a Static LibraryNow that you have created your library, you can now reap the benefits of it!.When you create a program, you can use any of the functions that you’ve included in your library by simply calling them and you don’t need to worry about defining them or including files for them separately upon compilation.
When you compile your program, you can simply provide the name of your program and the name of your library to the gcc command (along with any desired warning flags).
When you do so, you should use the special -l flag, leave out the prefix and .
The linker will attach these parts back to the name to create a name of a file to look for.
You should also use the -L flag to tell the linker that the library might be in standard locations where the compiler looks for system libraries OR in the current directory.
As illustrated above, your linker will be able to link all object files in the linking phase so your program becomes a complete executable.
compiling my program with my libraryexecuting the programFinal thoughtsSomething that I’ve also started to learn about is dynamic libraries.
One big difference between dynamic libraries and static ones are that dynamic libraries are actually linked in two stages as opposed to one.
During the linking phase, the linker will verify that all symbols required by the program are either linked directly to it or in one of the dynamic libraries.
Then, when the program is started, a dynamic loader checks which dynamic libraries were linked, loads them in memory, and attaches a copy to the program.
While programs that use dynamic libraries are a bit slower in terms of launching because of the complexity of dynamic loading, they also take less memory then programs built with static libraries because the same copy of the library can be loaded for multiple programs.
I am definitely excited to learn more about them.
In the meantime, I am very happy to now be able to use static libraries as a toolkit for my functions!.. More details