Reputation: 4576
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)?
Upvotes: 33
Views: 30487
Reputation: 23294
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
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.
Upvotes: 5
Reputation: 42842
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
<LANG>_CPPCHECK
target property
This property is supported only when
<LANG>
isC
orCXX
.Specify a ;-list containing a command line for the
cppcheck
static analysis tool. The Makefile Generators and the Ninja generator will runcppcheck
along with the compiler and report any problems.This property is initialized by the value of the
CMAKE_<LANG>_CPPCHECK
variable if it is set when a target is created.
Upvotes: 43