Listing Unused Symbols(列出未使用的符号)
问题描述
我想从一个大型项目中删除死代码,并希望从未使用的符号开始.有没有办法让链接器列出它已优化的未使用符号?我将 GNU 链接器 (LD) 与 GCC 一起使用.
I want to remove dead code from a largish project and would like to start with unused symbols. Is there anyway to get the linker to list unused symbols that it has optimized out? I am using the GNU linker (LD) together with GCC.
如果做不到这一点,任何 Binutils(readelf 或 objdump)都可以执行相同的功能吗?
Failing that, can any of the Binutils (readelf or objdump) perform the same function?
推荐答案
大多数编译器/链接器会优化未使用的符号.如果您在 *nix 系统上运行,您可以尝试对所有目标文件使用命令nm",对其进行过滤和排序,以生成由这些目标文件定义的所有导出函数的列表.
Most compilers/linkers optimise out unused symbols. If you're running on a *nix system, you can try using the command "nm" on all the object files, filter it and sort it, to produce a list of all exported functions defined by those object files.
nm *.o | grep "^[0-9a-f]* T " | sed 's/^[0-9a-f]* T //' | sort -u > symbols_in.txt
我相信您可以对最终的二进制文件执行相同的操作.
I believe you can do the same on the final binaries.
然后,如果您对两组结果进行比较,您应该会得到所有未使用的导出函数的列表.
If you then diff the two sets of results you should get a list of all unused exported functions.
请注意,由于条件编译而被排除的代码可能会使用某些函数.例如.#ifdef 开关表示在平台 A 上使用某某内置功能,而在另一个平台上使用您自己的函数版本,因为没有内置或标准库等效,或者它无法正常工作.
Beware though that some functions may be used by code that is excluded as a result of conditional compilation. E.g. a #ifdef switch to say that on platform A, use such and such built in functionality and on another platform use your own version of the function because there is no built in or standard library equivalent, or it doesn't work properly.
这篇关于列出未使用的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:列出未使用的符号


基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09