How to build a shared object to be loaded at run time?
From: Ramon (ramon_at_conexus.net)
Date: 01/05/05
- Next message: Dan Mercer: "Re: why does tar close stdout and stderr right before exit?"
- Previous message: Rich Teer: "Re: deamonizing a program in Unix"
- Next in thread: Jens.Toerring_at_physik.fu-berlin.de: "Re: How to build a shared object to be loaded at run time?"
- Reply: Jens.Toerring_at_physik.fu-berlin.de: "Re: How to build a shared object to be loaded at run time?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 5 Jan 2005 10:22:45 -0800
This is my first time trying to load a dynamic library
at run time. I have been using the example provided in
the 'dlsym' Linux man page (see it below).
The example below runs fine for me, but when I try to
execute my own library, the program dumps core. Again:
the cosine example runs perfectly well.
I suspect that the problem is that perhaps I don't know
the details of building the *.so library properly. So,
what is the correct procedure to I build a library such
as the math library mentioned below?
Thanks a lot, I really need to solve this...
-Ramon F Herrera
-----------------------------------------------------------------------------
EXAMPLE
Load the math library, and print the cosine of 2.0:
#include <stdio.h>
#include <dlfcn.h>
int main(int argc, char **argv) {
void *handle;
double (*cosine)(double);
char *error;
handle = dlopen ("libm.so", RTLD_LAZY);
if (!handle) {
fprintf (stderr, "%s\n", dlerror());
exit(1);
}
cosine = dlsym(handle, "cos");
if ((error = dlerror()) != NULL) {
fprintf (stderr, "%s\n", error);
exit(1);
}
printf ("%f\n", (*cosine)(2.0)); <- core dumped here
dlclose(handle);
return 0;
}
If this program were in a file named "foo.c", you would build
the program with the following command:
gcc -rdynamic -o foo foo.c -ldl
----------------------------------------------------------------------------
- Next message: Dan Mercer: "Re: why does tar close stdout and stderr right before exit?"
- Previous message: Rich Teer: "Re: deamonizing a program in Unix"
- Next in thread: Jens.Toerring_at_physik.fu-berlin.de: "Re: How to build a shared object to be loaded at run time?"
- Reply: Jens.Toerring_at_physik.fu-berlin.de: "Re: How to build a shared object to be loaded at run time?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|