I am looking at some C/C++ code that appears to have functions that are defined, but never used. This is a pretty tedious process to trace through the code and verify. I've done some surveys and there are a number of tools that can do this type of analysis.

Can anyone share any experiences or tips on which tools and techniques are best or alternative methods of verifying this code is in fact not used?


There are some tools out there which can find 'dead code' in your programs. you can read about them on Stack Overflow threads here and here. a small summary:

use the gcc compiler flags -Wunused and -Wunreachable-code, and then use a tool like lcov to find the unused methods.

  • 4
    Not sure what facilities for this exist in your compiler, but be aware that if you're using invocation based on any type of reflection or RTTI, it's possible to call a method by name that's not referenced statically anywhere in the codebase. – Mason Wheeler Jul 23 '12 at 19:00

Trace? Why? Just comment them out and run through the compiler. It will quickly tell you if you commented out some function which was referenced somewhere else.

  • 5
    It's wort to note that this will only work in compiled languages, and not interpreted ones like PHP. Also, it's impractical for any large project, as it could take you days to actually comment/uncomment all the methods, and check them. – Davor Ždralo Jul 23 '12 at 9:18
  • 2
    He clearly specifies that he is in C++. Incremental rebuilding is extremely fast- if all you've done is comment one method in one source file, the linker can tell you very pronto. – DeadMG Jul 23 '12 at 12:52
  • In C this should mostly work. But there are complications like code that uses DEFINEs to switch between different implementations. In C++ that won't work reliably due to function overloading, template specialization and probably a couple of other features. – CodesInChaos Jul 2 '16 at 18:08

If you are actively looking for unused code, use something Jake223 suggested.

But, if you just stumble upon some code that seems to be unused, you can just do a search for the method's name in your codebase. If it's a private method, your job is easy, search only the current class. If it's a public method, search everything. If it's a whole class, search for it's name in all your code.

No hit? Perfect. Delete the code and run tests. You do have tests, don't you? Than commit your changes to your document version system. You use one, don't you? That way if you discover, ever, that you need it back, you just revert a change.

Deleting code should be something you do every day. Maybe you refactor some code and delete the old one. Or find old, unused code and delete it. And even if you don't have backups, how hard it is to rewrite a function or two? And the second time you will certainly write them better than before.

  • +1 for suggesting the good, old fashioned, tedious process of manually searching for the function names. Not a lot of fun, but it works! – user53019 Jul 23 '12 at 11:37
  • 1
    And it's also not that tedious. Any modern IDE can very smartly search for any text. Morever, for C, C++, Java and even some degree for PHP, you can just right click a function and hit "Find usage". – Patkos Csaba Jul 23 '12 at 14:46

You can use Cppcheck for this purpose:

$ cppcheck --enable=unusedFunction .
Checking foo.c...
1/2 files checked 0% done
Checking main.c...
2/2 files checked 0% done
[foo.c:1]: (style) The function 'foo' is never used.

Unless you're looking for trouble (ie; you know they should be called) why bother. If they are genuinely uncalled, they will probably be removed by the linker, and in any case the wasted space is not significant. On the other hand, if you remove them and later find they were needed (perhaps in some compiler configuration you were not aware of) you will have more work to do to recover them. This is especially difficult if the removal and rediscovery are separated by a lot of time.

  • 4
    The reason is that if they are uncalled, then you are maintaining code that shouldn't be maintaining, and therefore wasting valuable engineering time. – Michael Kohne Jul 23 '12 at 0:29
  • When you are working on something , yes there will be many uncalled functions so you may need to keep them around even if your compiler warns that they shouldnt be around. A good idea would be to comment them out if there is less likelyhood of being used. But add a clear marker which tells that this code is not junk, add a date , and if you are a part of a team, whom to contact before cleaning them out. – DPD Jul 23 '12 at 4:57
  • 1
    As per DO-178, unused functions which are not justified by design should no be present. – jinawee Jul 16 '19 at 9:23

My IDE of choice is Eclipse, and while it's clunky and a bit of a pain to set up at first, it's well worth the effort considering all of the tools I get. One such tool (and I've no idea what it's official name is) informs you about unused code, such as classes, functions, variables, etc. The IDE simply displays a yellow line underneath the declaration. I'm not 100% sure if this works with multi-file projects, but you could always give it a shot!


IDEs usually have this feature well implemented and it works by default (it can usually be disabled).

Whatever method you pick from the other answers to find dead code, always be aware of reflection issues. Some methods/fields may be accessed through reflection alone. Removing those methods will not trigger any alarm, except on runtime.

If you have (good) tests written for your projects, they will be of great value in this situation.

Not the answer you're looking for? Browse other questions tagged or ask your own question.