Identifying Unused Functions in C/C++

14,496

Solution 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

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

Share:
14,496
JavaMan
Author by

JavaMan

Updated on June 17, 2022

Comments

  • JavaMan
    JavaMan almost 2 years

    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.