Cppcheck - C/C++ Code Analysis Tool

Find Tips and tricks on how to better use the Zeus IDE. Feel free to post your own tips but please do not post bug reports, feature requests or questions here.
Post Reply
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Cppcheck - C/C++ Code Analysis Tool

Post by jussij »

Cppcheck is a tool for C/C++ code analysis: https://cppcheck.sourceforge.io/

Installation
To install the software, download and run the Windows 64-bit installer found on that page.

Thie installer requires admin rights as it installs to the following folder location:

Code: Select all

C:\Program Files\Cppcheck
The installer does not add the installation folder to the system PATH so this will need to be done as a seperate user step as described here: viewtopic.php?t=6176

Cppcheck User Manual
The user manual for this tool can be found here: https://cppcheck.sourceforge.io/manual.pdf

Zeus Integration
To use cppcheck from inside Zeus, use the Options, Document Types menu to edit the C/C++ Document Type and inside the Tools panel add new tool using the details shown below:

Code: Select all

Cppcheck '$f' File
cppcheck.exe
--platform=win64  "$fn" 
$fdd
This will create a new C/C++ specific tool as shown below:
cppcheck.png
cppcheck.png (134.01 KiB) Viewed 26838 times
Testing the Cppcheck Tool
To test the newly created tool, save the following code to an example.cpp file:

Code: Select all

int main()
{
  char a[10];
  a[10] = 0;
  return 0;
}
With that file open in Zeus the Tools menu will now show the cppcheck option in the menu as shown below:
cppcheck-menu.png
cppcheck-menu.png (40.11 KiB) Viewed 26838 times
Running that tool will then result in the following output, highlighting the error in the code:
cppcheck-err.png
cppcheck-err.png (37.96 KiB) Viewed 26838 times
Cheers Jussi
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Re: Cppcheck - C/C++ Code Analysis Tool

Post by jussij »

The cppcheck utitlity can be used to test projects and solutions using command lines that take those details as shown below:

Code: Select all

cppcheck --project="foo.vcxproj"
cppcheck --project="bar.sln"
One issue this can cause is when the project code contains an #error directive, for example code similar to this:

Code: Select all

// Release mode dynamic link multi-threaded dll C-runtime
#if !defined(_MT) || !defined(_DLL)
    #error The /MDd compiler switch is required.
#endif
Running cppcheck on that code can result in the following error:
cppcheck-defines.png
cppcheck-defines.png (45.27 KiB) Viewed 26821 times
The issue here is because those preprocessor values are not defined the #error is triggered and the cppcheck aborts any further checks.

To fix this the -D command line option can be used to define these preprocessor values as shown below:

Code: Select all

cppcheck -D_DLL -D_MT --project="foo.vcxproj"
cppcheck -D_DLL -D_MT --project="bar.sln"
Jussi
Post Reply