Widely Vision

Archive for February 19th, 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

run:

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.

run:

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

output:

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.

regards,

aboelnour.

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.
———————————————
Example:
We will make a library contain 2 functions one print “Hello” the second print a given String.
here is the Files:

/*sayhello.c*/
#include <stdio.h>

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

/*saysomething.c*/
#include <stdio.h>
void saysomething(char *string)
{
printf(“%s\n”,string);
}

/*say.c*/
#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()) {
printf(“%s\n”,error);
exit(1);
}
sayhello = dlsym(handle,”sayhello”);
if(error = dlerror()) {
printf(“%s\n”,error);
exit(1);
}
saysomething = dlsym(handle,”saysomething”);
if(error = dlerror()) {
printf(“%s\n”,error);
exit(1);
}
sayhello();
saysomething(“This is something”);
dlclose(handle);
}

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:

./a.out

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.
run:

export LD_LIBRARY_PATH=”/home/fakeroot/Desktop”

the run

./a.out

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 :D

————————-

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.

regards,

aboelnour

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

 
Follow

Get every new post delivered to your Inbox.