Click here to Skip to main content
Click here to Skip to main content

Poor Man’s Visual Studio Cppcheck Integration

, 9 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Simple and free Cppcheck integration into Visual Studio.

Introduction

Cppcheck is a good tool to have in your arsenal. Anything that helps me avoid stupid mistakes is very welcome. The problem is that if you use Visual Studio, you either have to use the separate Cppcheck GUI or pay an arm and a leg for something like Visual Lint. If you want a more convenient way to run Cppcheck on your code, but don't want to shell out $200 for the privilege, there's a fairly easy way to do a simple integration.

This article is a re-post from http://avitebskiy.blogspot.com/2012/10/poor-mans-visual-studio-cppcheck.html

Getting Started

First things first, download the latest version Cppcheck from http://cppcheck.sourceforge.net/ and install it. This will give you both the command line version and the GUI version of Cppcheck.

Create a Visual Studio External Tool

In Visual Studio, open menu Tools→External Tools...

  • Click the Add button
  • Set the Title, for example Cppcheck
  • Set Command to C:\Program Files (x86)\Cppcheck\cppcheck.exe
  • Set Arguments to --quiet --verbose --template=vs $(ItemPath)
  • Set Initial Directory to $(ItemDir)
  • Make sure Use Output window checkbox is enabled
  • Click on the Move Up button repeatedly until your entry is at the top of the list, this will make it easier to identify you new command as you can count on it being called Tools.ExternalCommand1
  • Click OK.

Voila!  You now have a Cppcheck entry in your Tools menu:

 

Use the Tool

You can now use select the Cppcheck menu entry any time you want to run Cppcheck on a file.  The cool thing about the --template=vs switch is that you can click on a Cppcheck error and Visual Studio will automatically take you to that line of code:

Automate Cppcheck to Run on File Save

Now that we have created a command to run Cppcheck, we can have it run automatically after a file save:

  • Go to menu ToolsMacrosMacros IDE, this will bring up a the Macros IDE
  • On the left hand side there should be a Project Explorer panel
  • In the Project Explorer panel, double click on EnvironmentEvents module
  • Insert the following code in the module:
Private Sub DocumentEvents_DocumentSaved(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentSaved
    If Document.Language = "C/C++" Then
        DTE.ExecuteCommand("Tools.ExternalCommand1")
        Document.Activate()
    End If
End Sub
  • Now save the module, close the Macros IDE, and you're good to go

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Aleksey Vitebskiy
Software Developer TerraGo Technologies
United States United States
No Biography provided
Follow on   Twitter   Google+

Comments and Discussions

 
Questionplug-in for visual studio 2005 [modified] PinmemberMember 299120423-Apr-14 18:34 
QuestionCppcheck integration with VC2010 [modified] Pinmemberhari111r25-Mar-14 22:26 
Questioncppcheck intgration with GIT PinmemberMember 1054589223-Jan-14 20:22 
GeneralMy vote of 5 PinmemberDonChunior28-Jan-13 22:31 
QuestionGreat article! How to adjust the macro for "save all"? PinmemberPachner28-Jan-13 4:59 
AnswerRe: Great article! How to adjust the macro for "save all"? PinmemberAleksey Vitebskiy29-Jan-13 4:52 
GeneralMy vote of 5 PinmemberDrABELL9-Oct-12 7:23 
GeneralMy vote of 5 PinmemberAnna-Jayne Metcalfe8-Oct-12 4:39 
QuestionGood article PinmemberAnna-Jayne Metcalfe8-Oct-12 4:35 
A simple, concise & well illustrated article. I particularly like the File|Save macro idea, though that is obviously not viable in Visual Studio 2012 as the Macros IDE has been removed in that version of Visual Studio.
 
I'll declare an interest right now as I'm the original author of Visual Lint[^]. Incidentally although most of our customers are using PC-lint rather than CppCheck we could always do with more Visual Lint + CppCheck beta testers (if you're interested please feel free to drop me a line).
 
One thing you might also want to consider mentioning in the article is that there are two ways to run CppCheck, either without symbols/includes (as you have here) or with symbols and includes. The CppCheck author recommends running without either, and in that mode it is the fastest but still provides a reasonable level of issue detection by comparison to other modes.
 
However, it can fail with the dreaded "Too many configurations" error if the code being analysed is sufficiently complex. If that happens you will find that you need to feed it additional -D and -I parameters (preprocessor symbols, project includes etc.) on the command line. It still analyses pretty quickly when configured in this way, but obviously the configuration starts to get tricky if your projects are complex.
 
We've also found that if you feed system includes for CppCheck it slows right down (actually becoming far slower than PC-lint) for what appears to be little benefit. Hence we recommend either running CppCheck without symbols/includes, or only with project symbols/includes but not system includes.
Anna Rose | [Rose]
 
Tech Blog | Visual Lint

"Why would anyone prefer to wield a weapon that takes both hands at once, when they could use a lighter (and obviously superior) weapon that allows you to wield multiple ones at a time, and thus supports multi-paradigm carnage?"

AnswerRe: Good article PinmemberAleksey Vitebskiy8-Oct-12 5:37 
GeneralRe: Good article PinmemberAnna-Jayne Metcalfe8-Oct-12 5:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 9 Oct 2012
Article Copyright 2012 by Aleksey Vitebskiy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid