Skip to content

About

This repository aims for a tutorial of how to use cppcheck in your project for static testing.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Cppcheck Tutorial

Introduction

Cppcheck is a static analysis tool for C/C++ code that focuses on detecting undefined behavior and dangerous coding constructs. Unlike traditional compilers, Cppcheck is designed to identify bugs that might not be caught during the compilation process, aiming to minimize false positives.

Table of Contents

  1. Features
  2. Installation
  3. Basic Usage
  4. Advanced Usage
  5. Integration with Build Systems
  6. Graphical User Interface
  7. Resources

Features

Cppcheck offers a range of features to enhance code quality:

  • Detection of Out-of-Bounds Errors: Identifies array index violations.
  • Class Code Checks: Analyzes class structures for potential issues.
  • Exception Safety: Checks for exception-related problems.
  • Memory Leak Detection: Highlights possible memory leaks.
  • Obsolete Function Usage Warnings: Alerts about deprecated functions.
  • Invalid STL Usage: Detects improper use of Standard Template Library components.
  • Uninitialized Variables and Unused Functions: Finds variables that are used without initialization and functions that are declared but not used.

Installation

Windows

  1. Download: Obtain the latest Cppcheck installer from the official website.
  2. Install: Run the .msi installer and follow the on-screen instructions.
  3. Verify Installation:
    cppcheck --version

Linux

Using Package Manager:

  • Debian/Ubuntu:
    sudo apt-get install cppcheck
  • Fedora:
    sudo yum install cppcheck

Building from Source:

  1. Clone Repository:
    git clone --branch 2.4.x https://github.com/danmar/cppcheck.git /cppcheck
  2. Build and Install:
    mkdir /cppcheck/build
    cd /cppcheck/build
    cmake ..
    cmake --build . -- -j$(nproc)
    sudo make install
  3. Verify Installation:
    cppcheck --version

macOS

Cppcheck can be installed via Homebrew:

brew install cppcheck

Verify by running:

cppcheck --version

Basic Usage

To analyze a single file:

cppcheck filename.c

For recursive analysis of all source files in a directory:

cppcheck path/to/directory

To enable all checks and output results to a file:

cppcheck --enable=all path/to/directory 2> results.txt

Advanced Usage

Custom Rules

Cppcheck allows the creation of custom rules to enforce specific coding standards. Detailed guidance on writing custom rules can be found in the Cppcheck manual.

Suppressing Warnings

To suppress specific warnings, use the --suppress flag:

cppcheck --suppress=warningId path/to/file.c

For project-wide suppression, create a suppression file and reference it:

cppcheck --suppressions-list=suppressions.txt path/to/project

Integration with Build Systems

Cppcheck can be integrated with various build systems:

  • CMake: Generate a compile_commands.json file and run:
    cppcheck --project=compile_commands.json
  • Visual Studio: Analyze entire solutions or individual project files:
    cppcheck solution.sln

For comprehensive integration instructions, refer to the Cppcheck manual.

Graphical User Interface

Cppcheck offers a GUI for users who prefer a visual interface:

  • Windows: The installer includes cppcheck-gui.exe.
  • Linux/macOS: Build the GUI from source by enabling the BUILD_GUI option during the CMake configuration.

Resources

About

This repository aims for a tutorial of how to use cppcheck in your project for static testing.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages