1
我有一個程序調用dlopen(使用RTLD_NOW)來動態加載一個在運行時指定完整路徑但在程序第一次執行時不知道的庫。指定的庫與ANOTHER .so文件動態鏈接,該文件的位置在程序啓動之前還不知道,但在調用dlopen之前已知。關於如何讓這種情況發揮作用的任何想法?謝謝!dlopening一個依賴的庫
我有一個程序調用dlopen(使用RTLD_NOW)來動態加載一個在運行時指定完整路徑但在程序第一次執行時不知道的庫。指定的庫與ANOTHER .so文件動態鏈接,該文件的位置在程序啓動之前還不知道,但在調用dlopen之前已知。關於如何讓這種情況發揮作用的任何想法?謝謝!dlopening一個依賴的庫
它傳遞給方法在所述第一動態加載庫
three.c:
#include <stdio.h>
void
doit()
{
printf("Success\n");
}
two.c:
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
void go(const char* location)
{
void *handle;
void (*doit)(void);
char *error;
handle = dlopen(location, RTLD_LAZY);
if (!handle) {
fprintf(stderr, "%s\n", dlerror());
exit(EXIT_FAILURE);
}
dlerror();
*(void **) (&doit) = dlsym(handle, "doit");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", error);
exit(EXIT_FAILURE);
}
(*doit)();
dlclose(handle);
}
main.c中:
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int
main(int argc, char **argv)
{
void *handle;
void (*go)(const char*);
char *error;
handle = dlopen("/tmp/two.so", RTLD_NOW);
if (!handle) {
fprintf(stderr, "%s\n", dlerror());
return EXIT_FAILURE;
}
dlerror();
const char* location = "/tmp/three.so";
*(void **) (&go) = dlsym(handle, "go");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", error);
return EXIT_FAILURE;
}
(*go)(location);
dlclose(handle);
return EXIT_SUCCESS;
}
結果:
svengali ~ % gcc -o /tmp/two.so two.c -shared -rdynamic -fpic
svengali ~ % gcc -o /tmp/three.so three.c -shared -rdynamic -fpic
svengali ~ % gcc -rdynamic -o go main.c -ldl
svengali ~ % go
Success
svengali ~ %