There are two types of libraries in C, static libraries and dynamic, or shared libraries. Both have their advantages and disadvantages. We use libraries because they contain standardized code that has been perfected by thousands of users. This code is generally faster and safer then user generated code. It is also more portable, meaning it can be used on more systems then user defined code. User made libraries are also useful as they can contain code that will be used repeatedly, or used by multiple programs.
Libraries contain predefined code that is used throughout the program calling the library. This can save space, typing, and time. A well written library can reduce the file size and speed up the processing time of the program it is included in. This predefined code can be written by the user or can be from a standard library. A library works by including the appropriate code when it is called. There are two ways to do this depending on if the library is static or dynamic.
Static libraries are compiled into the program, this makes it so the code contained in the library to be used as functions in a program. This allows the program to look up the library and execute the code contained within. The library is part of the code. Static libraries do have disadvantages though, they have to be recompiled every time there is a change made to them, they take up space in the compiled program, and programs have to be closed in order for the changes made to the libraries to take effect.
Dynamic libraries are similar to static libraries in that they contain code that is used by the program, however they are not compiled into the program and are not necessarily part of the program when it is written. Instead think of them as street addresses, and those addresses contain the code that is to be executed. So the program has to stop and look up the code in a separate file to be able to execute the function call it is given. This is slower then a static library, however it saves space, a dynamic library can be easily changed, and the program does not necessarily have to shut down to be able to use the changes made to the library it need only reload the library.
To make our own libraries we first have to turn our .c code into object files, we will only be covering how to do this on Linux machines. There are two ways to do this depending on what type of library we want to make. To make a static library we just type gcc -c “filename(s)”.c and this will make object files suitable for a static library. To make a dynamic object files suitable for a dynamic library we have to include the -fpic flag, so that would be gcc -c -fpic “filename(s)”.c. The -c flag tells the compiler that we want object code, the -fpic flag tells the compiler we want position independent code, basically this second flag allows the code to be stored anywhere and still work. Once we have our object files, we can go on to create the appropriate library.
To create a static library we enter the command ar lib”library name”.a “names of .o files to include in library” into the terminal, if you would like to read more about static libraries I have written an entire post on them here.
To created a dynamic library we enter the following code into the terminal: gcc -fpic *.c -shared -o lib”library name”.so, the -o flag allows us to name the output file, we can use other flags in addition to these such as -Wall, -Werror and many others, but for the sake of this post I have decided to omit them for simplicity's sake.
Finally we want to use our libraries we have created, to do this we simply include them in our code, and then compile with them. To do this we must #include <library name> in our c code, and then compile them with the code. To do this we can either compile the code the following way for static libraries: gcc -o “output program name” “code”.c lib“library name”.a or for a dynamic library we can enter the following: gcc -o “output program name” “code”.c “library name”.so.