我正在编译一个大项目。这个项目正在使用共享库,尤其是 lapack 的。
对于给定的函数,我想确定系统在哪个共享库中找到了它。
这里是 nm 输出:
$ nm -DC ~/bin/app | grep potrf
U dpotrf_
正如预期的那样,dpotrf_ 是未定义的。
这里是 objdump 的结果:
$ objdump -TR ~bin/app | grep potrf
0000000000925428 R_X86_64_JUMP_SLOT dpotrf_
所以 objdump 找到了一些东西!是否有任何选项可以显示它在哪个共享库中找到它?或者另一个程序来做到这一点?
最佳答案
ldd 绝对是寻找候选库的起点。这是我在 .bashrc 中用于此类目的的内容(不漂亮,但符合我的目的)。
基本上,我对子目录中的所有库(.a、.so)执行 nm。如果 nm 为搜索到的符号生成输出,我将打印库名称和 nm 的相关行。然后,您的最后一步是搜索以“T”开头的行,因为这些行将您的符号定义为程序代码(文本)。
# run nm on a set of objects (ending with the 1st parameter) and
# grep the output for the 2nd parameter
function nmgrep ()
{
for i in $( find \. -name \*$1 ); do
if [[ ! -e $i ]]; then
continue;
fi
nm $i | grep $2 > /tmp/foo.tmp;
if [[ -s /tmp/foo.tmp ]]; then
echo $i;
cat /tmp/foo.tmp | grep $2
fi
rm /tmp/foo.tmp
done
}
# find symbols definied/referenced in libs
function libgrep ()
{
nmgrep .a $@
nmgrep .so $@
}
关于c - 如何查找外部函数定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4163668/