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

后端 未结 2 1725
忘掉有多难
忘掉有多难 2021-01-04 19:34

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

相关标签:
2条回答
  • 2021-01-04 20:14

    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.

    0 讨论(0)
  • 2021-01-04 20:15

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

    0 讨论(0)
提交回复
热议问题