Cppcheck support in CMake
Asked Answered
S

2

31

I am not asking about the various available third-party modules that support Cppcheck in one way or the other.

With CMake 3.10, CMake seems to have gained some official Cppcheck support. See CMAKE_<LANG>_CPPCHECK.

Unfortunately the documentation on how to use this variable is a bit sparse. Is there a good example of how Cppcheck is supposed to be used with CMake 3.10 (or later)?

Sou answered 5/2, 2018 at 15:6 Comment(0)
F
41

An simple example would be - if you have cppcheck in your PATH and you are not specifying additional parameters - the following by setting global CMAKE_<LANG>_CPPCHECK variable:

cmake_minimum_required(VERSION 3.10)

project(CppCheckTest)

file(
    WRITE "main.cpp"
[=[
int main()
{
    char a[10];
    a[10] = 0;
    return 0;
}
]=] 
)

set(CMAKE_CXX_CPPCHECK "cppcheck")
add_executable(${PROJECT_NAME} "main.cpp")

The files to scan are added automatically to the cppcheck command line. So the above example gives the following output (gcc and cppcheck on Linux system):

# make
Scanning dependencies of target CppCheckTest
[ 50%] Building CXX object CMakeFiles/CppCheckTest.dir/main.cpp.o
Checking .../CppCheckTest/main.cpp...
Warning: cppcheck reported diagnostics:
[/mnt/c/temp/StackOverflow/CppCheckTest/main.cpp:4]: (error) Array 'a[10]' accessed at index 10, which is out of bounds.
[100%] Linking CXX executable CppCheckTest
[100%] Built target CppCheckTest

You could give cppcheck a try in an existing project by simply setting the CMAKE_CXX_CPPCHECK variable via the cmake command line:

# cmake -DCMAKE_CXX_CPPCHECK:FILEPATH=cppcheck ..

A more "daily life" example would probably for you to include something like the following code snippet in your CMakeList.txt:

find_program(CMAKE_CXX_CPPCHECK NAMES cppcheck)
if (CMAKE_CXX_CPPCHECK)
    list(
        APPEND CMAKE_CXX_CPPCHECK 
            "--enable=warning"
            "--inconclusive"
            "--force" 
            "--inline-suppr"
            "--suppressions-list=${CMAKE_SOURCE_DIR}/CppCheckSuppressions.txt"
    )
endif()

References

Fourflush answered 5/2, 2018 at 19:47 Comment(9)
Thanks for the detailed answer, unfortunately it doesn't work here (Ubuntu 17.10, cppcheck 1.80 cmake 3.9.1. There is no output from cppcheck. How to debug this?Sou
@Sou Are you sure that this also works with CMake 3.9.x? Havn't checked this. I'm using CMake 3.10.x. The commit I've linked is 5 month old, so I suspect that it only works in 3.10.x (also if I look at the release notes it was added with Version 3.10).Fourflush
you are right, i misread the release notes, it works with 3.10Sou
I would like the compile to fail if there is acppcheck error or warning. I tried --error-exitcode=1 but unfortunately it did not have any effect. ( I know I can roll my own cppcheck target of course - but would be nice if it worked with the built in support ).Home
The "daily life" example provided does not work (at least using a Ninja generator with CMake 3.12.4)! It seems that CMAKE_CXX_CPPCHECK has to be fully specified on the CLI. Otherwise the build system does not pick up the value of CMAKE_CXX_CPPCHECK. I had it almost working by setting the variable manually as a CACHE variable in my CMakeLists.txt, but even then configure has to be run twice in order for the generator to pick up the updated CMAKE_CXX_CPPCHECK value. Guess: Build files are written in wrong order. I'm afraid the only portable solution is a custom target.Downdraft
Interestingly, when make is invoked with parallel jobs -jX, cppcheck output is garbled and ureadable.Coppock
I also try this but does not work for me with the latest cmake version. cppcheck is found on configure but when I build the proejct i did not get any cppcheck output. Did anyone has a link to a sample project?Pagel
I am also not getting the cppcheck output using the provided "daily life" example. mgiaco and @Fourflush Wolter, Did you guys got the fix to make that work? Could you also look at hereBethannbethanne
There's a easily overlooked point. The CMAKE_CXX_CPPCHECK must be defined BEFORE defining the target. Maybe this is the reason of your problem. @FlorianWoltersBeldam
S
3

Starting with CMake 3.5, setting CMAKE_EXPORT_COMPILE_COMMANDS to ON creates compile_commands.json in the build directory. Since version 1.76 Cppcheck can open such CMake project files with --project=.

Example
Usually, you configure and build (using make) your project like this:

mkdir build
cmake <config options> ..
make

To run Cppcheck, you have to adjust:

mkdir build
cmake <config options> -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
cppcheck --project=compile_commands.json --enable=all

Personal opinion
For manual uses, the feature to tell CMake to run Cppcheck (by setting CMAKE_<LANG>_CPPCHECK) is not useful for manual Cppcheck runs. This feature is meant to be used in a continuous testing system.

Stereopticon answered 21/10, 2023 at 22:50 Comment(2)
Curious that this seems to be undocumented.Lamere
It is documented, both in CMake and the Cppcheck manual. Or what are you meaning.Stereopticon

© 2022 - 2024 — McMap. All rights reserved.