Static analysis, finding unused code

This is more of a C++ question than a juce question, but they’ll roast me for dinner if I ask it on stack overflow.

Can anyone help me find tool that will automatically find unused code for a large project (i.e. functions that are never called)? I’ve spent hours trying to do this, but no success. I tried cppcheck, but it’s not coping well with multiple files. Visual Studio has a built in feature called Run Code Analysis, but I can’t get it to do anything at all. When I press the button, nothing happens, and it sometimes even messes up my build targets. I also can’t find any good documentation on this feature.

Does anyone have experience with Visual Studio Static Analysis? Or can you recommend any other tools?

1 Like

What issues is cppcheck giving you? I always run cppcheck, cpplint and clang-tidy before every commit.

1 Like

As far as I can tell, it was just running an analysis on each file, but not the whole project. This meant that it was identifying lots of functions as “unused” which were actually used in other cpp files. I tried making a simple unity build, i.e. a file which includes all of the cpp files, and running the analysis on that. But it still gave me complaints, and I wasn’t getting any results, so I gave up.

What build system are you using? CMake or the Projucer?


In that case, you can create a CompileCommands.json file by setting the CMake variable CMAKE_EXPORT_COMPILE_COMMANDS to ON. You also need to use either the Makefile or Ninja generators. Once you’ve created this file, you can tell cppcheck to analyze the whole project by calling cppcheck --project=<buildDir>/CompileCommands.json


I’ve set the cmake flag, but how do I use Ninja to create this file?

It should automatically output this file in the build directory if that variable is set and you’re using the Make or Ninja generators

1 Like