This post explains how to use static and shared libraries with C. My platform is gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5).
So what is static and shared library.
- Static library is an archive of object codes which you can combine with your c codes while compiling which will become a part of executable.
- Shared library : is not a part of executable. When you run your executable and when it requires it loads that library. So path to library folder have to be pre set all time. For eg: LD_LIBRARY_PATH in Linux.
- So no more theories Just Do It.
First we need libraries. Here I am going to explain two approaches; static library approach and shared library approach. So I am going to create a print library, which contains a function which prints Don't settle. So create a folder for our tutorial; mkdir lib_tut; cd lib_tut. Create a file named print.c and add the following into it.
#include < stdio.h >
void printmessage ( void )
{
printf("Don't settle\n");
}
Now to create a static library libprint.a containing this function, just do the following. Every library name should start with lib (eg: libprint.a or libgcc.a why, we will see it later in this post).
#include < stdio.h >
void printmessage ( void )
{
printf("Don't settle\n");
}
Now to create a static library libprint.a containing this function, just do the following. Every library name should start with lib (eg: libprint.a or libgcc.a why, we will see it later in this post).
- gcc -c print.c
- ar rs libprint.a print.o
The first line will generate print.o from print.c and the second line will archive it to libprint.a
And to create a shared library
- gcc -c -fpic print.c
- gcc -shared -o libprint.so print.o
'pic' is position independent code; an object code format required for shared libraries.
So we have static and shared libraries libprint.a and libprint.so respectively. So we can use it in our program. So before that create a header file which is a prototype for all the functions in your library. So create a file print.h and add the following line into it.
- extern void printmessage ( void );
First lets see how to use static library in our program. Create a file main.c and add the following code piece into it.
#include<print.h>
int main ( void )
{
printmessage();
return 0;
}
Now compile it
- gcc --static -I./ -L ./ -o app main.c -lprint
The option static says compile in static which means library will be a part of executable (By default the compilation is shared version). Option 'I' tells the compiler to look at this directory for header files. In this case for print.g Option 'L' tells to search for libraries here. Option 'l' (-lprint) tells the library to use. As the name is print the compiler will look for libprint.a (".a" as it is static compilation).
- Now execute the app ( ./app )
One situation is that you may want to use the static version of your library. But there can be many other libraries which your program is dependent. So in that case compile like the following.
- gcc -I./ -L ./ -o app main.c ./libprint.a
Lets use the shared library approach. To compile
- gcc -I./ -L ./ -o app main.c -lprint
This will generate app without static linking as by default compilation is shared approach. But when execute this it will give an error like this
- libprint.so: cannot open shared object file: No such file or directory.
To execute this you have to export your libprint.so path like the following.
- export LD_LIBRARY_PATH = $LD_LIBRARY_PATH:/home/arun/lib_tut
In the above line replace /home/arun/lib_tut with yout libprint.so path (Path can be set in ~/.bashrc also). So while executing, the executable will search this path also and it gets the library and it executes.