Re: [off-topic] Loadable modules

Brian Candler (B.Candler@pobox.com)
Sun, 1 Jun 1997 20:55:03 +0100 (BST)


Thank you everyone. libdl does exactly what I want, much more neatly than I
had hoped for (although I should have expected this from Linux :-)

Cheers,

Brian.

(short example attached)

:::::::: wibble.c
#include <stdio.h>

void entry(void)
{
printf("Hello, world!\n");
}

:::::::: main.c
#include <dlfcn.h>
#include <stdio.h>

int main(void)
{
const char *e;
void (*p)();

void *h = dlopen("./wibble.o", RTLD_NOW);
if (!h) {
printf("Failed: %s\n", dlerror());
return 1;
}
p = dlsym(h, "entry");
e = dlerror();
if (e) {
printf("Failed(2): %s\n", e);
return 1;
}
p();
dlclose(h);
return 0;
}

Compile like this:

gcc -Wall main.c -o main -ldl
gcc -Wall -shared wibble.c -o wibble.o
./main