How to build a shared object to be loaded at run time?

From: Ramon (ramon_at_conexus.net)
Date: 01/05/05


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

----------------------------------------------------------------------------



Relevant Pages

  • Re: Server.Transfer goes to wrong page
    ... long-running pages as a countdown during load. ... it was a client-side redirect, I never caught it in my debugger. ... >>preserveForm), the first time, it loads up BasicReports again. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Loading of a Live CD stops?
    ... This is a further complicated situation because I know that the CD worked in the drive the first time I used it to boot from, then after that, it has always given me the error I reported. ... Kailesh MUSSAI wrote: The error seems to be that modprobe is not able to load the module ... I get it when I try to load an Ubuntu v7.10 Live CD. ...
    (Ubuntu)
  • Re: 3 Textures loaded but shader finds only one...
    ... fprintf (stderr, "failed to load %s! ... exit; ... texturemapfile, this->textureMap); ...
    (comp.graphics.api.opengl)
  • Cosmosworks expressions
    ... I'm doing some analysis in CosmosWorks for the first time. ... I have multiple cases to study (static load at maximum weight, ... The part that I can't figure out is the syntax used for expressions ...
    (comp.cad.solidworks)
  • Re: OT Browser
    ... > if you block the spyware the site won't load. ... I prefer the antivirus called "MacOSX", but in the Windows world, AVG ... > First time you do a system scan with either one, ...
    (rec.crafts.metalworking)

Loading