Using GCC to find unreachable functions (“dead code”)

懵懂的女人 提交于 2019-11-30 02:42:10

问题


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!


回答1:


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.




回答2:


gcov is what you're looking for. You have that listed in the question, have you not looked at it?



来源:https://stackoverflow.com/questions/4195494/using-gcc-to-find-unreachable-functions-dead-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!