Identifying Unused Functions in C/C++ [duplicate]

旧街凉风 提交于 2019-12-08 00:44:53

问题


Possible Duplicate:
Finding “dead code” in a large C++ legacy application

My project has a lots of C source files each with a lots of global functions. Many of these are no longer referenced by any caller at all. Is there a simple way to identify which of these functions are not referenced by anyone at all?

The map file generated by VC seems to be useful. But I am not sure exactly how/when a function name is listed in the map file.


回答1:


You can use CCCC (free, open source) which gives you lots of metrics about your program. Another option would be Coverity (not free).

This question may be a duplicate of this one: Dead code detection in legacy C/C++ project




回答2:


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.)



来源:https://stackoverflow.com/questions/10732832/identifying-unused-functions-in-c-c

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