我有两个文件:
foo.c:int foo() { extern int bar(); return bar(); }bar.c:int bar() { extern int missing(); return missing() + 42; }
我编译它们,并组成.a静态库:

$ gcc -c foo.c bar.c
$ ar rcs libfoobar.a foo.o bar.o

我想找到整个文件中缺少的符号(未定义)。但是我仍然将bar设为undefined,尽管它存在于foo.o中:
$ nm -u libfoobar.a
foo.o:
    U bar
bar.o:
    U missing

如何从输出中省略bar并仅显示missing

最佳答案

将整个文件链接到一个目标文件中,并检查:

ar -x libfoo.a      # unpack libfoo.a
ld -r -o tmp.o *.o  # link all objects into tmp.o
nm -u tmp.o         # find undefined symbols in tmp.o
rm *.o              # remove tmp.o as well as the contents of libfoo.a

当链接器解析它可以在-r请求的部分链接中找到的所有符号时,这应该给出期望的结果。

请注意,当前工作目录不应包含任何目标文件,以获得最佳效果。

您可以使用以下脚本获得更一致的结果:
# use: script libfoo.a
tmp=${TEMPDIR:-/tmp}/undefsyms.$$
mkdir $tmp
cp $1 $tmp/lib.a
cd $tmp
ar -x lib.a
ld -r -o $$.o *.o
nm -u $$.o
rm *
cd ..
rmdir $tmp

如果您为此感到不方便地解压缩和链接库,请使用以下脚本,该脚本使用命令行实用程序来使用join实用程序来计算所需的信息:
if [ $# -lt 1 ]
then
    echo Usage: $0 library.a
    exit 1
fi

lib=$1

postproc() { cut -w -f 2-3 | sort -u; }
allsyms() { nm -g -P -A $lib | postproc; }
undefsyms() { nm -g -P -A -u $lib | postproc; }
defsyms() { allsyms | grep -v 'U$'; }

comm -2 -3 <(undefsyms | cut -w -f 1) <(defsyms | cut -w -f 1)

07-24 09:44
查看更多