Creating and linking static and shared libraries

In this post, i will explain how to create, link and access static and shared libraries.

Libraries can be created as
1. static library (.a)
2. shared library (.so)

Static library (*.a files) – these libraries are linked to application at compile time and become part of application itself. This increases the size of application.
Shared libraries (*.so files) – Most of us are aware of loading and unloading of dynamic libraries with dlopen(), but there is another method of linking the dynamic libraries. Shared libraries can be accessed in two ways.
a.Dynamically loaded/unloaded, using the dynamic linking loader system function
b.Dynamically linked at run time but statically aware. The libraries must be available during compile/link phase. The shared objects are not included into the executable component but are tied to the execution.

Example for creating static and shared library
———————————————-
//mean.c – file containing a method to calculate the mean
#include
double mean(double a, double b) {
return (a+b) / 2;
}

//mean.h header file
double mean(double, double);

Create object file from source file
#gcc -c mean.c -o mean.o

Create static library, libmean.a
#ar -cvq libmean.a mean.o
the library name must start with the three letters lib and have the suffix .a

Create shared library , libmean.so
#gcc -c -fPIC mean.c -o mean.o
#gcc -shared -Wl,-soname,libmean.so.1 -o libmean.so.1.0.1 mean.o
the library must start with the three letter lib

How to use the static and shared libraries in program
—————————————————–
//main.c where mean() defined in static/shared library is called
#include
#include “mean.h”

int main(int argc, char* argv[]) {

double v1, v2, m;
v1 = 5.2;
v2 = 7.9;

m = mean(v1, v2);

printf(“The mean of %3.2f and %3.2f is %3.2f\n”, v1, v2, m);

return 0;
}

Statically linked
—————–
#gcc -static main.c -L. -lmean -o statically_linked

Dynamically linked at run time but statically aware
—————————————————
#gcc main.c -o dynamically_linked -L. -lmean

Dynamically loaded/unloaded
—————————
In this method, compile the program with no reference to shared library. Shared library will be loaded/accessed/unloaded with the system calls dlopen(), dlsym() and dlclose(). Following is how this is done.

Following is the main.c file for dynamically loading and unloding shared libray
#include
#include “mean.h”

int main(int argc, char* argv[]) {

double *lib_handle;
double v1, v2, m;
v1 = 5.2;
v2 = 7.9;

lib_handle = dlopen(“/opt/lib/libmean.so”, RTLD_LAZY);
if (!lib_handle)
{
fprintf(stderr, “%s\n”, dlerror());
exit(1);
}

fn = dlsym(lib_handle, “mean”);
if ((error = dlerror()) != NULL)
{
fprintf(stderr, “%s\n”, error);
exit(1);
}

m = (*fn)(v1,v2);

printf(“The mean of %3.2f and %3.2f is %3.2f\n”, v1, v2, m);

return 0;
}
//compile the program
#gcc main.c -L. -o dynamically_linked

For more detailed information with example, refer to following link
http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
http://www.adp-gmbh.ch/cpp/gcc/create_lib.html

Advertisements
This entry was posted in C Section. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s