Setting up cppcheck

To install pre-built cppcheck: http://cppcheck.sourceforge.net/

sudo apt-get update
sudo apt-get install cppcheck

In case, you want to use latest cppcheck, download and build it from the source code

wget https://github.com/danmar/cppcheck/archive/2.1.tar.gz
tar zxvf 2.1.tar.gz
cd cppcheck-2.1
mkdir build
cd build
cmake ..

P.S: In case you are interested for cppcheck GUI use cmake -DBUILD_GUI=ON ..

make
sudo make install

Once installation is complete.

which cppcheck
/usr/local/bin/cppcheck

cppcheck --version
Cppcheck 2.1

Build your cpp project with cmake and pass on an extra parameter -DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE to cmake. It will produceĀ  compile_commands.json file in your build folder for the project.

Execute cppcheck -p </path/to/folder/having/compile_command.json>.

To use it in VSCode add following to the tasks.json

{
    "label": "Run cppCheck",
    "command": "/usr/local/bin/cppcheck",
    "args": [
       "--project=${workspaceRoot}/build/compile_commands.json"
    ],
    "problemMatcher": "$gcc",
}

Ref: https://gist.github.com/aakbar5/268a2072138345893b7f82590dcc3d26