Everything you need to know about static libraries.
First of all, it is important to know what a library is in #C and what is a function in #C.
A function is a group of statements that together perform a task. Every #C program has at least one function, which is main(), and all the most trivial programs can define additional functions.
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.
The #C standard library provides numerous built-in functions that your program can call. For example, strcat() to concatenate two strings, memcpy() to copy one memory location to another location, and many more functions.
The general form of a function definition in C programming language is as follows :
return_type function_name( parameter list ) { body of the function
}
Library is simply a collection of functions which can be added to your application and the functions called as necessary, just like any other functions in the application. More precisely any object, not only functions can be stored in a library, but the vast majority of libraries only contain functions.
There are various types of libraries, in #C and C++ there are these:
- Static library. (lib) Compiled to object code by the compiler and the objects merged into a library by the linker/librarian tool. (usually coming with the compiler suite)
- Dynamic linked library. (DLL) This is a windows specific thing. When loading the application, the operating system automatically links the application with the library. The linking operation is basically the same as the normal linking of executables, the difference is that the final executable is created when loading the application, not when building it.
- DLL or other library file opened by the application and the functions called through function pointers. (A.K.A shared library concept on other systems) Typical use case is the plugin system for many applications. The plugins are basically libraries.
After knowing what a library is in #C and what is a function in #C , we can talk about static libraries.
Static libraries are simply a collection of ordinary object files; conventionally, static libraries end with the ".a'' suffix. This collection is created using the ar (archiver) program. Static libraries aren't used as often as they once were, because of the advantages of shared libraries (described below). Still, they're sometimes created, they existed first historically, and they're simpler to explain.
Why use libraries
Static libraries permit users to link to programs without having to recompile its code, saving recompilation time. Note that recompilation time is less important given today's faster compilers, so this reason is not as strong as it once was. Static libraries are often useful for developers if they wish to permit programmers to link to their library, but don't want to give the library source code (which is an advantage to the library vendor, but obviously not an advantage to the programmer trying to use the library). In theory, code in static ELF libraries that is linked into an executable should run slightly faster (by 1-5%) than a shared library or a dynamically loaded library, but in practice this rarely seems to be the case due to other confounding factors.
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 util_file.o util_net.o util_math.o
This command creates a static library named 'libutil.a' and puts copies of the object files "util_file.o", "util_net.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.
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 (this will be better understood when we take a deeper look at the link process at the end of this tutorial). The command used to create or update the index is called 'ranlib', and is invoked as follows:
ranlib libutil.a
How to use a static library
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 prog
This will create a program using object file "main.o", and any symbols it requires from the "util" 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.
Hope you have been able to understand everything about static libraries.
Software Developer
4 年It's a foundation! A very important thing to know, which will help you build better software.