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 »

Archive for February, 2010

Deeper in Libraries With GCC(4)

Posted by aboelnour on February 19, 2010

peace be upon you:

After making your library you will need to edit it or just read it’s Contents.
Listing Symbols Names in Object Files:
The nm utility can be used to list all the symbols defined in (or referenced from) an object file, a static archive library, or a shared library. If no file is named on the command line, the file name a.out is assumed


nm libc.a

look the man page for more info about options.
Removing Unused Information from Object Files:
The strip utility removes the debugging symbol table information from the object file or files named on the command line. The object file can be a static library, a shared library, or a .o file produced by the compiler. Depending on how much debugging information has been included in the file, stripping can dramatically reduce the size of the file.


strip main.o libglom.a

this command will strip all debugging information
from the object file main.o and all the object files in the library libglom.a

The strip utility replaces the existing file with the stripped version, so if you want to be able to restore the original unstripped versions, you will need to save the files before stripping them or use the -o option to produce the output in a different file.
Listing Shared Library Dependencies:
The ldd utility reads through the object files in the binary executable or shared library named on the command line and lists all the shared library dependencies.

For example,the following command lists the shared libraries used by the bash shell program on a Linux system:

ldd /bin/bash


linux-gate.so.1 =>  (0xb8022000)
libncurses.so.5 => /lib/libncurses.so.5 (0xb7fc3000)
libdl.so.2 => /lib/tls/i686/cmov/libdl.so.2 (0xb7fbf000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7e60000)
/lib/ld-linux.so.2 (0xb8008000)

this is the shared objects which the bash use it the ldd utility determine the library and it’s path on your hard.

that’s the end of our series.I hope that any one find it helpful. Any question is welcomed.



Posted in C/C++, linux | 2 Comments »

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.



Posted in C/C++, linux | Leave a Comment »

Deeper in Libraries With GCC(2)

Posted by aboelnour on February 9, 2010

peace be upon you:

It is a long time since i wrote here. sorry for the few who following this blog 😀

In last post we talked about libraries and how to make a static one.

today we will continue our series in the libraries.

Example on static libraries:

libhello.c File:

#include <stdio.h>

void hello(void) {
printf(“Hello, library world.\n”);

libhello.h File:

void hello(void);

demo_use.c File:

#include “libhello.h”

int main(void) {
return 0;

then run in terminal:

gcc -Wall -g -c -o libhello-static.o libhello.c

to obtain the object file of the source file which we want to make it a library.

ar rcs libhello-static.a libhello-static.o

to create the library.

gcc -Wall -g -c demo_use.c -o demo_use.o

to obtain the object file of the source which use the libarary.

gcc -g -o demo_use_static demo_use.o -L. -lhello-static

to compile the object file with the libarary.


to run the program.

and here you can make your own static lib and deal with it.


Dynamic Linking:

In the static Linking we locating the library in the linking time this operation done by the linker which called by the compiler.

But there is another technique in linking calling dynamic linking which locating the libraries in the Run Time.

Once a program has been linked to use shared libraries, it must be able to find the shared library when it runs. The libraries are located by name, not by directory.

The loading of the libraries in the run time done by the Operating System.

to make the explanation easy we will start with an Example:

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <dlfcn.h>
  4. int main(int argc, char **argv) {
  5. void *handle;
  6. double (*cosine)(double);
  7. char *error;
  8. handle = dlopen (“/lib/libm.so.6”, RTLD_LAZY);
  9. if(error = dlerror()) {
  10. printf(“%s\n”,error);
  11. exit(1);
  12. }
  13. cosine = dlsym(handle, “cos”);
  14. if(error = dlerror()) {
  15. printf(“%s\n”,error);
  16. exit(1);
  17. }
  18. printf (“%f\n”, (*cosine)(2.0));
  19. dlclose(handle);
  20. }

In this Example we will calculate the cosine of (2.0) using a library called “libm.so.6” located in /lib.

Explanation of the code:

1-2: We include the standard library and standard I/O library.

3: We include the header dlfcn.h which contain the functions: dlopen(),dlsym(),dlerror(),dlclose() we will explain them later.

5-7: Declaration of void-char pointer 6: Declaration of Function pointer.

8: here we call the dlopen() function which loads the shared library into memory (if it’s not already there) and returns a handle that can be used to address it the first parameter is the library and the second one is a flag  passing to the Operating System.

9-12 ,14-17 : Handle Errors the dlerror() function returns a descriptive string describing the error that occurred on the most recent call to any one of the other functions(dlopen(), dlsym(), dlclose()). The dlerror() function returns NULL if no error occurred.

13: Here we call the dlsym() which return the addresses of the functions the first parameter is the address of the library and the second parameter is the name of the function which you want to use it from the library .

18: here we get the value of the cosine(2) with the function which we get from the library which loaded in the memory.

19: here we call the dlclose() Functions that detaches the current program from the shared library. If no other programs are attached to it,the dynamic library is unloaded from memory.

Note: The flag used as the second argument on the call to dlopen() can be RTLD_NOW,which causes all the functions in the library to be loaded into memory and become immediately available. The other option is to specify RTLD_LAZY, which will delay the actual loading of each function until it is referenced on a call to dlsym(). Either of these flags can be OR‘ed with RTLD_GLOBAL, which allows any external references in this library to be resolved by calling functions found in other (also loaded) dynamic libraries.

to build your program run:

gcc test.c -ldl -o testth

then to run:


the option -ldl indicate that this program will need the Dynamic linker of the OS.


so here we are we will cover in the next part how to make your own dynamic library and how to deal with the Environment variables and how to configure your library.


aboelnour 🙂

Posted in C/C++, linux | 3 Comments »

%d bloggers like this: