我如何找到静态库中定义符号的位置 [英] How do I find where a symbol is defined among static libraries

查看:524
本文介绍了我如何找到静态库中定义符号的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您正在使用包含多个工具和库的代码库,并且想要移植(或复活)此类代码库中的某些组件,但是有关符号位于各个库中的位置的任何线索要么丢失,要么需要很长的时间才能找出来.查看代码本身(是的,改进的文档可以避免此类问题,但要求很高).发现在哪个库中可以找到代码中使用的符号的最快方法是什么?

Suppose you work with a codebase comprising several tools and libraries and you want to port (or resurrect) some component within such codebase but any clue about where symbols lie within the various libs is either lost or will take ages to find out by looking at the code itself (yes improved documentation can avoid such issues but is quite demanding). What is the fastest way to discover in which library you can find symbols used in the code?

推荐答案

假设使用Linux操作系统,可以在库文件中列出名称的 nm 工具可以解决.

Assuming a linux box, the nm tool, listing names in library files, comes to the rescue.

它可以用来进行广泛的搜索,如下所示:首先可以找到所有可用的库(假设项目已成功编译,而没有添加您要添加​​的组件),然后可以将此类发现包含在一个库中.在所有发现的库上调用nm的循环;然后grep的输出将丢弃"U"引用(未定义的符号,也就是使用该符号的位置).在单个bash行上给出:

It can be used to do an extensive search as follows: one can first find all the libraries available (assuming the project have been successfully compiled without the component you are adding) with a find, then such find can be enclosed in a loop where you call nm on all discovered libraries; the output you then grep for discarding "U" references (undefined symbols, aka where else the symbol is being used). On a single bash line that gives:

for lib in $(find base_path -name \*.a) ; do echo $lib ; nm $lib | grep my_symbol | grep -v " U "   ; done

其中:

  • base_path是代码库的根
  • my_symbol是您要寻找的符号

echo会生成所有找到的库的列表,这不是很干净,因为它会输出不包含该符号的库的名称,但这是我发现直接引用该库的最快方法,因此当您看到一个:

The echo generates a list of all libraries found, which is not so clean since it outputs names of libs not holding the symbol, but it was the fastest way I found to have a direct reference to the library so when you see a:

base_path/component/libA.a
0000000000000080 D my_symbol

您已经找到了通常的嫌疑犯.

You have found your usual suspect.

这篇关于我如何找到静态库中定义符号的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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