【问题标题】:How do I print .so file location at runtime? [closed]如何在运行时打印 .so 文件位置? [关闭]
【发布时间】:2021-02-09 10:31:55
【问题描述】:

在 foo.c 中

void location(char *path)
{
    //to do
}

在 main.c 中

int main()
{
    char foopath[256];
    
    location(foopath);
    
    printf("%s\n",foopath);
}

也许它会显示/lib/foo.so

我觉得可以用ldd之类的shell脚本来获取路径,但是好像不太好看。

我想读取与foo.so 相同位置的文件。所以我需要正确的路径。

【问题讨论】:

标签: c linux shared-libraries dynamic-linking


【解决方案1】:

您可以使用“dl”库。显示“fopen”符号的动态库文件名的程序示例:

#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>

int main(void)
{
  int rc;
  Dl_info info;

  rc = dladdr(fopen, &info);
  if (rc) {
    printf("%s\n", info.dli_fname);
    return 0;
  }

  return 1;
}

$ gcc example.c -l dl
$ ./a.out
/lib/x86_64-linux-gnu/libc.so.6

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-05
    • 2010-09-18
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 2016-04-01
    • 2020-07-31
    • 1970-01-01
    相关资源
    最近更新 更多