|
|
Shared library file
b.c
#include
void b_printer ()
{
printf (“Printing something from a shared library.n”);
}
gcc -fPIC -shared -o b.so b.c
Prototypes for the Shared Library Functions Found in dlfcn.h
void* dlopen (const char* filename, int flag);
const char* dlerror (void);
void* dlsym (void* handle, char* symbol);
- Open the shared library by using dlopen().
- Check that the open succeeded by using dlerror().
- Get the function that you want by using dlsym().
- Make sure that the function lookup succeeded by using dlerror().
- When done with the library, close it by using dlclose().
You
must include -ldl in your command-line flags to gcc when compiling and
linking programs that call these functions (gcc -ldl sample.c).
#include
#include
/* using the dl* functions to load and use a shared library */
int main ()
{
void* handle;
void (*printer)(void);
char* error;
/* Open the shared library, ‘b.so’. */
handle = dlopen (“/home/chelf/linuxmag/0202/b.so”, RTLD_LAZY);
/* Check to see if there were any errors in opening the library. */
error = dlerror ();
if (error)
{
/* If so, print the error message and exit. */
printf (“%sn”, error);
exit (1);
}
/* Get the ‘b_printer’ function out of the library. */
printer = dlsym (handle, “b_printer”);
error = dlerror ();
/* Check to see if there were any errors in getting the function. */
if (error)
{
/* If so, print the error message and exit. */
printf (“%sn”, error);
exit (1);
}
/* Call the function. */
printer ();
/* Close the shared library. */
dlclose (handle);
}
gcc -fPIC -c b.c
This will generate a file, b.o, which you can then link with:
ld -shared -o b.so b.o
Now there are no link errors. You might have also noticed that the size of this new shared
library is much smaller than the original. This is because you haven’t
allowed gcc to put in all of the extra functions necessary for
executables as you would if you had used gcc -shared -o b.so b.o as the final step.
The ‘nm’ Utility
machine:~> nm b.so
00001448 A _DYNAMIC
00001438 A _GLOBAL_OFFSET_TABLE_
000014a8 A __bss_start
000014a8 A _edata
000014a8 A _end
000003a0 T _fini
00000344 T _init
00000370 T b_printer
00000340 t gcc2_compiled.
U printf
another useful tool is nm. This program lists all of the symbols from an object file. Figure Five shows the output of nm when run on the shared library b.so. You’ll see that nm
displays much information (certainly enough to write an entire article
about),
but the interesting pieces are the lines that have a T and U in the
second column. T means that the symbol is defined in this object file
(_fini(), _init(), and b_printer() are the three functions in your shared library), and U means the function is undefined (printf() is undefined because it will be linked in from another shared library).
Related posts:
- linux watch command & example usage you can use watch command directly like this watch –interval=1...
- High CPU usage solution for vmware on linux (files on NTFS) If you have high cpu usage problem for your vmware...
- Maxtor Delivers Secure Remote Access to Your Shared Storage From any Web Browser LAS VEGAS-Seagate (NYSE: STX) announced at CES a free software...
- Linux i2c driver implementation An I2C chip driver controls the process of talking to...
- Disk Usage Information The CLI way The df utility displays the disk space...
Comments
Leave a comment Trackback