Home » Cool Tools » Cppcheck


posted by Andy Maloney

Cppcheck is an open source cool tool by Daniel Marjamäki used to statically analyse C/C++ code for errors, questionable code, and style. I’ve been tracking it for a number of months and, while it is still in active development, it is already a very useful tool. There are command line and GUI versions available for several platforms.

The Cppcheck wiki page gives a lot more information about it, and lists a couple of hundred checks that are performed on your code.

A git repository is available which makes it easy to keep up-to-date with the latest developments. The source also includes a Qt-based GUI which is quite straightforward to compile using Qt Creator. The GUI is functional, but could use a little bit of work to make it more friendly. In order to view error or warning in the source, for example, Cppcheck requires an external program. It really ought to show you the code in context in the main window, allowing you to correct things within the application.

As a quick example, here’s a bit of nasty code:

Running it through Cppcheck using the GUI, we get results like this:

Cppcheck Example

Cppcheck Example

You can see that, for this simple example, it picked up the outright error with a reference, a warning about initialization, and a question about style. As I mentioned above, it would be nice to be able to at least see the offending code in the window when you click on the error or warning. An option to keep the resulting list of warnings and errors expanded would be a welcome addition. It would also be nice to have the messages in the bottom section clickable so you could find a more detailed explanation with examples and solutions online. I’m sure these kinds of things will come as the project matures.

I highly recommend you check this tool out. It can catch a lot of things you and your compilers can easily miss and will help make your codebase more robust.

Leave a Reply

Your email address will not be published. Required fields are marked *