该程序链接到哪个库提供给定的功能? [英] Which library the program is linked to that provides a given function?

查看:98
本文介绍了该程序链接到哪个库提供给定的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序调用库中定义的函数 foo 。我怎么知道库在文件系统中的位置? (像它是一个静态库或动态链接的库?)

更新:使用 ldd ,程序有很多依赖库。如何判断哪些库包含函数 foo

解决方案

请说出您所在的操作系统,答案依赖于系统。



在Linux和大多数UNIX系统上,您可以简单地让链接器告诉您。例如,假设你想知道 printf 是从哪里来的

  #include< stdio.h> 
int main()
{
return printf(Hello \\\
);


$ gcc -c tc
$ gcc to -Wl,-y,printf
to:引用printf
/ lib / libc。 so.6:定义printf

这告诉你 printf 中被引用,并且在 libc.so.6 中定义。以上解决方案适用于静态库和共享库。



由于您使用 gdb 标记了此问题,因此以下是你可以在gdb中完成:

  gdb -q ./a.out 
读取/ tmp / a中的符号。出...完成。

(gdb)b main
断点1在0x400528
(gdb)运行

断点1,0x0000000000400528在main()中
gdb)info symbol& printf
printf在/lib/libc.so.6部分.text中

如果 foo 来自共享库, gdb 会告诉你哪一个。如果它来自一个静态库(在这种情况下 gdb 将在a.out <.cf> 的.text节中说),使用上面的 -Wl,-y,foo 解决方案。您也可以这样做一个暴力解决方案:

  find / -name'* .a'-print0 | xargs -0 nm -A | grep'foo $'


I have a program invoking function foo that is defined in a library. How can I know where the library is in the file system? (like is it a static library or a dynamically linked lib?)

Update: with using ldd, the program has a lot of dependency library. How to tell which lib contains function foo?

解决方案

You didn't say which OS you are on, and the answer is system-dependent.

On Linux and most UNIX systems, you can simply ask the linker to tell you. For example, suppose you wanted to know where printf is coming from into this program:

#include <stdio.h>
int main()
{
  return printf("Hello\n");
}

$ gcc -c t.c
$ gcc t.o -Wl,-y,printf
t.o: reference to printf
/lib/libc.so.6: definition of printf

This tells you that printf is referenced in t.o and defined in libc.so.6. Above solution will work for both static and shared libraries.

Since you tagged this question with gdb, here is what you can do in gdb:

gdb -q ./a.out
Reading symbols from /tmp/a.out...done.

(gdb) b main
Breakpoint 1 at 0x400528
(gdb) run

Breakpoint 1, 0x0000000000400528 in main ()
(gdb) info symbol &printf
printf in section .text of /lib/libc.so.6

If foo comes from a shared library, gdb will tell you which one. If it comes from a static library (in which case gdb will say in section .text of a.out), use the -Wl,-y,foo solution above. You could also do a "brute force" solution like this:

find / -name '*.a' -print0 | xargs -0 nm -A | grep ' foo$'

这篇关于该程序链接到哪个库提供给定的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆