tag:blogger.com,1999:blog-9674987618021240822024-03-05T18:19:47.494-08:00ViteGeoRandom thoughts on programming...Anonymoushttp://www.blogger.com/profile/13627392831168430804noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-967498761802124082.post-31064815642028366082012-10-08T15:29:00.004-07:002012-10-08T15:33:56.784-07:00Poor Man’s Visual Studio Cppcheck Integration<div dir="ltr" style="text-align: left;" trbidi="on">
<a href="http://cppcheck.sourceforge.net/" target="_blank" title="Cppcheck">Cppcheck</a> 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 <a href="http://www.riverblade.co.uk/products/visual_lint/index.html" target="_blank" title="Visual Lint">Visual Lint</a>. 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.
<br />
<h3>
<br />
<a name='more'></a>
Getting Started</h3>
First things first, download the latest version Cppcheck from <a href="http://cppcheck.sourceforge.net/">http://cppcheck.sourceforge.net/</a> and install it. This will give you both the command line version and the GUI version of Cppcheck.
<br />
<h3>
<br />
Create a Visual Studio External Tool</h3>
<ul>
<li>In Visual Studio, open menu <strong>Tools→External Tools...</strong></li>
<img height="390" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHoXxIvL24cFBl6oaFwkHx4oOfSK6qwlhC61vlge0tPvCGSvkxRaMiSQulpjgLb7sehl2jNXVxflLGpj8NO3L-2Nsb0PO8qJ34ov-kRHD0apO6oMUnfXWRV84gMTzEmAi45QdQFrTbGMs/s400/VS_External_Tools.png" width="400" />
<li>click the <strong>Add</strong> button</li>
<li>set the <strong>Title</strong>, for example <em>Cppcheck</em></li>
<li>set <strong>Command</strong> to <em>C:\Program Files (x86)\Cppcheck\cppcheck.exe</em></li>
<li>set <strong>Arguments</strong> to<em> --quiet --verbose --template=vs $(ItemPath)</em></li>
<li>set <strong>Initial Directory</strong> to <em>$(ItemDir)</em></li>
<li>make sure<strong> Use Output window</strong> checkbox is enabled</li>
<li>click on the <strong>Move Up</strong> 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 <em>Tools.ExternalCommand1</em></li>
<li>click <strong>OK</strong>.</li>
</ul>
Voila! You now have a <strong>Cppcheck</strong> entry in your <strong>Tools</strong> menu:<br />
<img height="367" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg03UWrSUwe5HaHN2br0sKyXz8HX6wJelsmuo6BOja2Oh05HJGbx2A8QfPEHpZmbChXnZQoGiyfj-7WdEaLjATHZrjrM7l2JiH3es0qipZ-xKpcQ6LYNzxfUZbrCBYmB2CSlWl3j9iCQqM/s400/ExternalToolsMenu.png" width="400" />
<br />
<h3>
<br />
Use the Tool</h3>
You can now use select the <strong>Cppcheck</strong> menu entry any time you want to run Cppcheck on a file. The cool thing about the <em>--template=vs</em>switch is that you can click on a Cppcheck error and Visual Studio will automatically take you to that line of code:<br />
<img height="301" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmX20Ps_aLYS1cuKPcxU7Q4vD_ayYzvAlos6LVtAogFLmMb6lt3eyE3MsMY6MxH5fgjLQWjGNi13ZPHnrEwSiXIlh1AqVOFqCQvojGTEI-KGSobI0s3zNGDrvXqI2ctGC9oZGfTZqyHgk/s400/VS_with_cppcheck.png" width="400" />
<br />
<h3>
<br />
Automate Cppcheck to Run on File Save</h3>
Now that we have created a command to run Cppcheck, we can have it run automatically after a file save:<br />
<ul>
<li>go to menu <strong>Tools<strong>→</strong>Macros<strong>→</strong>Macros IDE</strong>, this will bring up a the <strong>Macros IDE</strong></li>
<li>on the left hand side there should be a <strong>Project Explorer</strong> panel</li>
<li>in the <strong>Project Explorer</strong> panel, double click on <strong>EnvironmentEvents</strong> module</li>
<li>Insert the following code in the module:</li>
</ul>
<pre class="brush: vb">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</pre>
<ul>
<li>now save the module, close the Macros IDE, and you're good to go</li>
</ul>
</div>Anonymoushttp://www.blogger.com/profile/13627392831168430804noreply@blogger.com0