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.
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
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.)
A straightfoward but laborious way of doing this is simply to wrap the function declaration and definitions of any suspect functions with preprocessor directives:
#if 0
void old_func();
#endif
...
#if 0
void old_func()
{
}
#endif
or if you want to have more control, you can replace 0 with and actual macro of your own so #if 0
becomes #ifdef NO_OLD_FUNCTIONS
- that way it's easier to find and take them out permenantly at a later stage once you're happy they're not used.
Naturally, you'll need to fully compile and link the code and any other code that builds on it.