Creating shared & static library using gnu compiler

The code for the library

calc_mean.c

//#include <stdio.h>

##N(mean)
double mean(double a, double b) {
  return (a+b) / 2;
}

The header file

Of course, we need a header file.

calc_mean.h

double mean(double, double);

Creating the static library

A static library is basically a set of object files that were copied
into a single file. This single file is the static library. The static file
is created with the archiver (ar).

First, calc_mean.c is turned into an object file:

gcc -c calc_mean.c -o calc_mean.o

Then, the archiver (ar) is invoked to produce a static library
(named libmean.a) out of the object file calc_mean.o.

ar  rcs libmean.a      calc_mean.o

Note: the library must start with the three letters lib and have the suffix .a.

Creating the shared library

As with static libraries, an object file is created. The -fPIC option tells gcc to create position independant code which is necessary for shared libraries. Note also, that the object file created for the static library will be overwritten. That’s not bad, however, because we have a static library that already contains the needed object file.

gcc -c -fPIC calc_mean.c -o calc_mean.o

For some reason, gcc says:

cc1: warning: -fPIC ignored for target
(all code is position independent)

It looks like -fPIC is not necessary on x86, but all manuals say,
it’s needed, so I use it too.

Now, the shared library is created

gcc -shared -Wl,-soname,libmean.so.1 -o
libmean.so.1.0.1  calc_mean.o

Note: the library must start with the three letter lib.

The programm using the library

This is the program that uses the calc_mean library. Once, we
will link it against the static library and once against the shared library.

main.c

#include <stdio.h>
#include "calc_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;
}

Linking against static library

gcc -static main.c -L. -lmean -o statically_linked

Note: the first three letters (the lib) must not be specified,
as well as the suffix (.a)

Linking against shared library

gcc main.c -o dynamically_linked -L. -lmean

Note: the first three letters (the lib) must not be specified,
as well as the suffix (.so)

Executing the dynamically linked programm

LD_LIBRARY_PATH=.
./dynamically_linked

Interrogating libraries (`which library is sin() in?’)

nm libraryname should list all the symbols that libraryname has references to. It works on both static and shared libraries. Suppose that you want to know where tcgetattr() is defined: you might do

$ nm libncurses.so.1 |grep tcget
         U tcgetattr

The U stands for `undefined’ — it shows that the ncurses library uses but does not define it. You could also do

$ nm libc.so.5 | grep tcget
00010fe8 T __tcgetattr
00010fe8 W tcgetattr
00068718 T tcgetpgrp

The `W‘ stands for `weak’, which means that the symbol is defined, but in such a way that it can be overridden by another definition in a different library. A straightforward `normal’ definition (such as the one for tcgetpgrp) is marked by a `T

The short answer to the question in the title, by the way, is libm.(so|a). All the functions defined in <math.h> are kept in the maths library; thus you need to link with -lm when using any of them.

Finding files

ld: Output file requires shared library `libfoo.so.1`

The file search strategy of ld and friends varies according to version, but the only default you can reasonably assume is /usr/lib. If you want libraries elsewhere to be searched, specify their directories with the -L option to gcc or ld.

ELF shared libraries

To build libfoo.so as a shared library, the basic steps look like this:

$ gcc -fPIC -c *.c
$ gcc -shared -Wl,-soname,libfoo.so.1
-o libfoo.so.1.0 *.o
$ ln -s libfoo.so.1.0 libfoo.so.1
$ ln -s libfoo.so.1 libfoo.so
$ LD_LIBRARY_PATH=`pwd`:$LD_LIBRARY_PATH ;
export LD_LIBRARY_PATH

Tags: ,


Did You Enjoy This Post?

Subscribe to our blog through our RSS feed or email to receive updates on more posts like this. post on your favourite social networking or media site to let others know about this post. Help us generate more buzz by submitting/voting for this post with the following buttons.

Leave a Reply


request new questions/article: send email to freequestionbank at gmail dot com