使用GCC查找无法访问的功能(“死码”) [英] Using GCC to find unreachable functions ("dead code")

查看:103
本文介绍了使用GCC查找无法访问的功能(“死码”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在非常大的C ++项目中寻找静态无法访问的函数的方法。我曾尝试过使用doxygen和其他静态分析工具,但似乎这个项目对于他们来说太复杂了。最后,我决定使用GCC工具(g ++,gprof,gcov等)是最安全的选择,但我无法弄清楚如何去做。
我认为g ++优化消除了静态无法访问的函数,但我不确定如何获取它消除的函数的名称。



您有任何建议吗? ?



感谢!

解决方案

死代码优化通常由链接器 - 编译器没有概述。但是,编译器可能已经消除了未使用的 static 函数(因为它们具有内部链接)。

因此,你应该不看海湾合作委员会的选项,但在ld选项。看来你想要 - print-gc-sections 。但是,请注意,您可能希望GCC将每个函数放在它自己的部分中, -ffunction-sections 。默认情况下,GCC会将所有函数放在同一个区域,而ld不够聪明以消除未使用的函数 - 它只能消除未使用的区域。

Hey guys, I was looking for a way of finding statically unreachable functions in a (very) big C++ project. I had tried using doxygen and other static analysis tools suggested here but it seemed that the project is too complicated for them to handle. Finally i decided that using GCC tools (g++, gprof, gcov, etc.) is the safest option, although i couldn't figure out how to do it. I think the g++ optimizations eliminate statically unreachable functions but I'm not sure how to get the names of the functions it eliminates.

Do you have any suggestions?

Thanks!

解决方案

Dead code optimization is typically done by the linker - the compiler doesn't have the overview. However, the compiler might have eliminated unused static functions (as they have internal linkage).

Therefore, you shouldn't be looking at GCC options, but at ld options. It seems you want --print-gc-sections. However, note that you probably want GCC to put each function in its own section, -ffunction-sections. By default GCC will put all functions in the same section, and ld isn't smart enough to eliminate unused functions - it can only eliminate unused sections.

这篇关于使用GCC查找无法访问的功能(“死码”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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