Pages

Sunday, November 1, 2015

How to integrate Cppcheck with Microsoft Visual Studio


  1. Download the latest version of Cppcheck from here.
  2. Open Microsoft Visual Studio IDE.
  3. In Visual Studio, open menu Tools -> External Tools.... The External Tools window will appear.
  4. Click Add button.
  5. Set Title field as Cppcheck (This can be set to any value).
  6. Set Command field to the path to cppcheck.exe (The ... button pops an Open File dialog box to select the required file).
  7. Set Arguments field to the parameters passed to cppcheck.exe (--verbose --enable=all --template=vs *.cpp *.h *.c).
    --verbose: Output more detailed error information.
    --enable=all : Enable all messages including errors, warnings, performance messages, information messages, unused function checks, style messages.
    --template=vs : Show the output in Visual Studio compatible format.
    For extended list of arguments supported by Cppcheck, refer user manual available here.
  8. Set Initial directory to $(ItemDir).
  9. Check Use Output window to redirect the Cppcheck outputs to the Visual Studio Output window.
  10. Click OKCppcheck should appear on the Tools menu.

To use Cppcheck, select Cppcheck menu entry on Tools menu.

Friday, October 23, 2015

Identify Memory Leaks in Visual CPP Applications

A friend of mine told me about this useful tool to identify hard to find memory leaks in Visual C++ applications. Visual Leak Detector (VLD) is an easy to use memory leak detection system. The installation package can be downloaded from here.

After installation, it can be used with any C/C++ project simply by adding the following line to the code:


#include <vld.h>



When the program is executed under the Visual Studio debugger, Visual Leak Detector will output a memory leak report of the executed segment of the code, at the end of the debugging session. If memory leaks are detected, this report will point to the exact locations in the code segment, which allocated the leaked memory block.

The header file can be easily isolated from rest of the source codes by guarding it with the pre-processor directive block. It can be made further user friendly by defining a separate Visual Studio build configuration for VLD. The steps are as follows.

Monday, October 19, 2015

Debugging with GDB


As discussed here, "printf debugging" is the most straight forward way of figuring out what is happening under the hood, during run time of an C / C++ program. But this approach is a bit time consuming as the code has to be modified and recompiled according to the debugging scenario. A more flexible way is to use a separate debugger program. GDB: The GNU project debugger stands at the top of this list. 

The general practice is to use GDB with an Integrated Development Environment as eclipse, which greatly simplifies the debugging process. However, to debug a program executing within a device which does not have a graphical user interface (for instance an embedded Linux system with a serial console), GDB on a console is a pretty useful option.