|
|
GDB Tutorial
GNU Make Tutorial
Library with GCC
Vi Tutorial
About This Tutorial
This tutorial explains how to create and use "Static and Shared Libraries" with GCC. A code Example is also provided. The example code is in "C" and has been tested on Linux.
Why Library?
A big application involves different modules or components. For example consider a Media Player. The application software running on the Media Player might have
- A User Interface for user interaction
- One or more Audio Decoders to decode different Audio Streams e.g. MP3,WMA,RM
- One or more Video Decoders
- And many more applications
Developing these different modules generally requires differnt kind of technical expertise. Hence mostly these software modules are developed by different teams of programmers. Later the software developed by these different teams is integrated in to a single application. Even with in a team there may be different small groups, developing small independent sections of code. Basically you have each team developing a number of source files [containing function definitions], which are finally to be used by another application. Under such scenarios, it is convenient to create a library of these functions. Library is a binary file obtained by archiving the different object files. In absence of a library, you will need to include all the source files (prvoided by each development team) in to the final application, which might be cumbersome to manage. Whereas use oflibraries eases the building, debugging and maitaining process for the final application.
Static and Shared Library
Library is an archive of object files. There are two ways of linking these object files to your final application.
- Static Linking: Linking is done statically and output of Linker is a binary executable, which can work stand-alone on the target.
- Dynamic Linking: Linker generates a binary executable, which can be run on the target in combination with Dynamically created library.
In this tutorial we will discuss how to create static and dynamic libraries using GCC and how to run link/run the final application.
Example Code
Download the example code provided with this example. The Example application contains two source files "funA.c" and "funcB.c" which will be archived in to a library. These library functions are later called from a file main.c. The header file "example.h" contains the declarations for the library functions.
Working with Static Libraries
(Refer to the directory STATIC in attached example)
- Creating object files
gcc -c funcA.c -o funcA.o
gcc -c funcB.c -o funcB.o
- Archiving the object files
ar rc libexample.a funcA.o funcB.o
- Linking the archive with application
gcc -static main.c -L. -lexample -o my_app.out
- Running an applicaiton created with Static Library
./my_app.out
Working with Shared Library
Refer to the directory SHARED in attached example
- Creating the object file
you need to create the object files with -fPIC option. PIC stands for position independent code. The -fPIC directs gcc to generate position independent code which is necessary for shared libraries.
gcc -fPIC -c funcA.c -o funcA.o
gcc -fPIC -c funcB.c -o funcB.o
- Naming Conventions
Every shared library has three differnet names
- linker name:is the name which linker uses. The name should have a prefix "lib", followed by name of the library, followed by ".so".
- soname: Is the name which is used while running the application. SONAME is the linker name, followed by a period, followed by a major version number
- real name: is the name of actual binary archive generated by the compler. The real name is the soname, followed by a period, a minor number, another period, and the release number. The last period and release number are optional.
- Creating the Shared Library
gcc -shared -Wl,-soname,libexample.so.1 -o libexample.so.1.0.1 funcA.o funcB.o
- Creating links
ln libexample.so.1.0.1 libexample.so
ln libexample.so.1.0.1 libexample.so.1
- Building the application
gcc main.c -L. -lexample -o my_app.out
- Exporting the library path
LD_LIBRARY_PATH=/opt/gdbm-1.8.3/lib
export LD_LIBRARY_PATH
- Running the application
./my_app.out
Shall I use a static or Dynamic linking?
When you have multiple application running on your system, each of which uses the same library, then using the shared library avoids the need of multiple images of the library. In case you use the static library, each EXE file will have an image of the same library and hence you will have a larget memory foot print.
|
|