2

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.

Community
  • 1
  • 1
JavaMan
  • 4,494
  • 4
  • 34
  • 62
  • Manually delete and recompile the code for the doubtful functions. – iammilind May 24 '12 at 07:18
  • Actually, I am interested in learning how the map file generated by the VC linker can be used for this purpose. (since it is freely available) – JavaMan May 24 '12 at 07:20

2 Answers2

4

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

Community
  • 1
  • 1
Brady
  • 9,873
  • 1
  • 18
  • 56
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.)

James Kanze
  • 142,482
  • 15
  • 169
  • 310
  • Using compiler and linker options to create individual sections for each function and global variable and to throw away unused code would generate a map file with only the referenced symbols. You could then remove all non mentioned public functions and the static functions referred by them, which most compilers would complain about anyway. – Spidey Apr 22 '20 at 09:59