Widely Vision

  • Real History of Israel

  • Poll

  • Get a PDF version

  • Follow This Blog

  • February 2010
    S M T W T F S
    « Nov   Jun »

Deeper in Libraries With GCC(3)

Posted by aboelnour on February 19, 2010

peace be upon you:
In last Post we talk about how to deal with the dynamic library in this post isA we will talk about how to make your Dynamic library and how to configure it.
We will make a library contain 2 functions one print “Hello” the second print a given String.
here is the Files:

#include <stdio.h>

void sayhello()
printf(“Hello from a loaded function\n”);

#include <stdio.h>
void saysomething(char *string)

#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc,char *argv[])
void *handle;
char *error;
void (*sayhello)(void);
void (*saysomething)(char *);
handle = dlopen(“libsay.so”,RTLD_LAZY);
if(error = dlerror()) {
sayhello = dlsym(handle,”sayhello”);
if(error = dlerror()) {
saysomething = dlsym(handle,”saysomething”);
if(error = dlerror()) {
saysomething(“This is something”);

then run:

gcc -fpic -shared sayhello.c asaysomething.c -o libsay.so

to make your shaerd object.

then to build your prog:

gcc -ldl say.c

to run:


this message will appear:

libsay.so: cannot open shared object file: No such file or directory

that’s mean that the Linker couldn’t find your library that’s lead to we have to know how the linker search for the libraries.
the Dynamic Linker search by default in the directories /lib    /usr/lib.
and there is an environment variable called LD_LIBRARY_PATH.
this var contain the paths which the DL will search throw it so you need to edit it with the path of the directory which contains the library.

export LD_LIBRARY_PATH=”/home/fakeroot/Desktop”

the run


you will notice that the program works.

Another important environment variable called LD_PRELOAD this var is very useful because the linker load any library locate in this var before loading any library he should load.
With this var you can override some libraries in Linux like the libraries which is Responsible of detecting the user’s id 😀


Configuring the Search for Shared Libraries:
In this part we will talk about the ldconfig utility.
The ldconfig utility performs two fundamental functions dealing with shared libraries.
First, it creates links so that references to shared libraries are always to the latest version.
Second, it stores a complete list of the available shared libraries in the file /etc/ld.so.cache.

The ldconfig utility reads the file /etc/ld.so.conf, which is a list of
directories containing shared libraries, and uses these directory names (along with the directories /lib and /usr/lib) to locate the libraries to be linked and listed in /etc/ld.so.cache.
The following command will create all the new links necessary and generate
a new version of the file /etc/ld.so.cache (you must be root):

ldconfig -v

for more information:

man ldconfig


In next post I will finish this series isA.




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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: