2

可能重复:
在大型 C++ 遗留应用程序中查找“死代码”

我的项目有很多 C 源文件,每个文件都有很多全局函数。其中许多根本不再被任何调用者引用。有没有一种简单的方法来识别这些函数中的哪些函数根本没有被任何人引用?

VC生成的地图文件似乎很有用。但我不确定函数名称是如何/何时在地图文件中列出的。

4

2 回答 2

4

您可以使用CCCC(免费、开源),它可以为您提供有关您的程序的大量指标。另一种选择是Coverity(不是免费的)。

这个问题可能与这个问题重复:Dead code detection in legacy C/C++ project

于 2012-05-24T07:23:48.923 回答
1

I don't think that the map file will be of any use. If it's like other map files I've seen, it won't indicate where (if anywhere) a symbol is used—only where it is defined. What you can do is run dumpbin over your object files: dumpbin /relocations, for example, will in fact display every use of a symbol with an address which may need relocation (practically speaking, functions and variables with static lifetime). You then use your usual tools on the output to determine whether the function you are interested in is there or not. (As someone who has worked mostly on Unix, I've installed CygWin and would use grep. I'm not familiar with the native equivalents to the different Unix tools under Windows.)

It would be fairly simple (using Python, or some similar scripting language) to write a small script which would parse the output of dumpbin /symbols for each of your object files, to get the names of all of the functions you've defined, then parses the output of dumpbin /relocations to give you a list of the functions you use, and finally does the diff of the two. (Microsoft seems to have gone out of their way to make the output of dumpbin difficult to use, but it's still not that difficult; you just have to know which lines to ignore.)

于 2012-05-24T07:48:36.780 回答