您可以为此使用递归版本的 ldd 吗?似乎有人写了一个可能有帮助的脚本。这至少告诉您所有依赖库都可以解析,如果它们首先在 .so 中正确指定。您可以使用链接器选项保证在 .so 中引用所有依赖项,并且这个加上递归 ldd 将保证您没有未解析的符号。
链接器通常可以选择使共享库中未解析的符号成为错误,您可以使用它来完全避免检查。对于 GNU ld,您可以只传递 --no-allow-shlib-undefined 并且保证如果它生成 .so,它不会有未解析的符号。来自 GNU ld 文档:
--no-undefined
Report unresolved symbol references from regular object files.
This is done even if the linker is creating a non-symbolic shared
library. The switch --[no-]allow-shlib-undefined controls the
behaviour for reporting unresolved references found in shared
libraries being linked in.
--allow-shlib-undefined
--no-allow-shlib-undefined
Allows (the default) or disallows undefined symbols in shared
libraries. This switch is similar to --no-undefined except
that it determines the behaviour when the undefined symbols are
in a shared library rather than a regular object file. It does
not affect how undefined symbols in regular object files are
handled.
The reason that --allow-shlib-undefined is the default is that the
shared library being specified at link time may not be the
same as the one that is available at load time, so the symbols might
actually be resolvable at load time. Plus there are some systems,
(eg BeOS) where undefined symbols in shared libraries is normal.
(The kernel patches them at load time to select which function is most
appropriate for the current architecture. This is used for example to
dynamically select an appropriate memset function). Apparently it is
also normal for HPPA shared libraries to have undefined symbols.
如果您要进行链接后检查,我同意 Martin 的观点,nm 可能是您最好的选择。我通常只是在输出中用 grep 搜索“U”来检查未解析的符号,所以我认为这将是一个非常简单的脚本。