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.

Tuesday, March 11, 2014

Preprocessor to control printf debugging

A fundamental way of debugging an application, especially developed using  C / C++ programming languages is "printf debugging". i.e. To dump a message on to console output with some variables.

However, including too many "printf debugging" statements can hinder the performance of an application. Therefore, it is important to include a mechanism to automatically remove all such debugging statements. The easiest way to achieve this is through conditional compilations.

One straight forward way is to wrap the printf statements within a preprocessor directive as follows.


#ifdef _DEBUG
    printf("Debug message\n");
#endif


This approach adds too much clutter into the source code. Therefore, I was looking for a elegant solution to this fundamental problem and came across the following code block.

Monday, December 23, 2013

Catching Linux signals - Example C program

Did a quick test on a strip down Linux machine to make sure the basics are in tact before troubleshooting a C program with signal handlers. This simple program triggers an alarm signal at a predetermined time. Just to make sure it has not stalled, a while loop is used to print the remaining time for the signal to trigger.    

 

#include <stdio.h>

#include <signal.h>

void handle(int sig)

{

   switch (sig)

   {

      case SIGALRM:

         printf("\nAlarm triggered\n");

         break;

      default:

         break;

   }

}



int main(int argc, char * argv[]) {

   int ticks = 10;

   signal(SIGALRM, handle);

   if (argc == 1)

   {

      printf("Using default alarm time: 10 seconds\n");

   }

   else

   {

      ticks = strtol(argv[1], NULL, 10);

      printf("Using alarm time: %d second(s)\n", ticks);

   }

   //Schedule alarm

   alarm(ticks);

   //Print remaining time

   while(ticks > 0)

   {

       printf("\r%02d", ticks);

   

       //Should call this function to display tick before sleeping.

       //Otherwise the request is buffered.

       fflush(stdout);

       ticks--;

       sleep(1);

   }

 

   //Cancel pending alarm requests, if any.

   alarm(0);

   printf("\ndone\n");



   return 0;

}