如何找到在静态库中定义符号的位置
假设您使用包含多个工具和库的代码库,并且您希望在此类代码库中移植(或重新生成)某些组件,但有关符号在各个库中的位置的任何线索要么丢失,要么需要很长时间才能找到查看代码本身(是的,改进的文档可以避免此类问题,但要求很高).发现在哪个库中可以找到代码中使用的符号的最快方法是什么?
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.
它可以用来做一个广泛的搜索,如下:可以先用一个find找到所有可用的库(假设项目已经成功编译,没有你要添加的组件),然后这个find可以包含在一个在所有发现的库上调用 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.
相关文章