Linux 打开具有依赖项的库

Linux 打开具有依赖项的库,linux,dynamic-linking,dlopen,ld-preload,Linux,Dynamic Linking,Dlopen,Ld Preload,我有一个程序,它调用dlopen(现在使用RTLD_)动态加载一个库,该库的完整路径在运行时指定,但在第一次执行该程序时未知。指定的库与另一个.so文件进行了动态链接,该文件的位置在程序启动后才知道,但在调用dlopen之前就知道了。关于如何让这个场景工作,有什么想法吗?谢谢 将其传递给第一个动态加载库中的方法 三、c: #include <stdio.h> void doit() { printf("Success\n"); } #include <stdio.h&

我有一个程序,它调用dlopen(现在使用RTLD_)动态加载一个库,该库的完整路径在运行时指定,但在第一次执行该程序时未知。指定的库与另一个.so文件进行了动态链接,该文件的位置在程序启动后才知道,但在调用dlopen之前就知道了。关于如何让这个场景工作,有什么想法吗?谢谢

将其传递给第一个动态加载库中的方法

三、c:

#include <stdio.h>

void
doit()
{
   printf("Success\n");
}
#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);
}
#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 ~ %