PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Shared Object .so -> Verfügbarkeit von Funktion



tommy@linux
10-11-2004, 10:05
hallo,

ich habe einen Manager geschrieben, welche .so laden kann. Ich gebe jeweils die Funktion an, welche er aus dem .so nutzen soll.
Das Problem ist, das ich irgendwie überprüfen muss, ob es die gewählte Funktion in diesem .so auch gibt, da er sonst natürlich bei falscher Funktion nichts findet und abstürzt.

Hat jemand eine Idee?

f0rtex
10-11-2004, 13:40
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

int main(int argc, char **argv) {
void *handle;
double (*cosine)(double);
char *error;

handle = dlopen ("/lib/libm.so.6", RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}

/* Für dich interessant */
cosine = dlsym(handle, "cos");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
exit(1);
}

printf ("%f\n", (*cosine)(2.0));
dlclose(handle);
}


greets
f0rtex

Joghurt
10-11-2004, 13:40
RTFM ;)

The function dlsym() takes a "handle" of a dynamic library returned by dlopen and the NUL-terminated symbol name, returning the address where that symbol is loaded into memory. If the symbol is not found, in the specified library or any of the libraries that were automatically loaded by dlopen() when that library was loaded, dlsym() returns NULL.
HTH

tommy@linux
10-11-2004, 16:32
danke erstmal. ;)

Gib es irgendwie die Möglichkeit die Funktionen aufzurufen, welche in dem .so implementiert sind? D.h. ich würde gerne eine Auswahl haben, von den Funktionen, welche das .so enthält.

locus vivendi
10-11-2004, 18:29
D.h. ich würde gerne eine Auswahl haben, von den Funktionen, welche das .so enthält.
Mach es doch einfach andersrum: Benutze nicht dlsym um jede einzelne Funktion zu finden, sondern tue in jede deiner *.sos eine spezielle Funktion rein, welche dir zurückliefert, welche Funktionen enthalten sind.