CMake 中的 Cppcheck 支持
Cppcheck support in CMake
我不是在询问以某种方式支持 Cppcheck 的各种可用第三方模块。
在 CMake 3.10 中,CMake 似乎获得了一些官方 Cppcheck 支持。参见 CMAKE_<LANG>_CPPCHECK。
不幸的是,关于如何使用这个变量的文档有点稀疏。是否有一个很好的例子说明 Cppcheck 应该如何与 CMake 3.10(或更高版本)一起使用?
一个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
变量:
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")
要扫描的文件会自动添加到 cppcheck
命令行。所以上面的例子给出了以下输出(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/Whosebug/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
您可以在现有项目中尝试 cppcheck
,只需通过 cmake
命令行设置 CMAKE_CXX_CPPCHECK
变量即可:
# cmake -DCMAKE_CXX_CPPCHECK:FILEPATH=cppcheck ..
更多 "daily life" 示例可能会让您在 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()
参考资料
- CMake Commit: Add properties to run cppcheck along with the compiler
<LANG>_CPPCHECK
target property
This property is supported only when <LANG>
is C
or CXX
.
Specify a ;-list containing a command line for the cppcheck
static analysis tool. The Makefile Generators and the Ninja generator will run cppcheck
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.
我不是在询问以某种方式支持 Cppcheck 的各种可用第三方模块。
在 CMake 3.10 中,CMake 似乎获得了一些官方 Cppcheck 支持。参见 CMAKE_<LANG>_CPPCHECK。
不幸的是,关于如何使用这个变量的文档有点稀疏。是否有一个很好的例子说明 Cppcheck 应该如何与 CMake 3.10(或更高版本)一起使用?
一个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
变量:
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")
要扫描的文件会自动添加到 cppcheck
命令行。所以上面的例子给出了以下输出(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/Whosebug/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
您可以在现有项目中尝试 cppcheck
,只需通过 cmake
命令行设置 CMAKE_CXX_CPPCHECK
变量即可:
# cmake -DCMAKE_CXX_CPPCHECK:FILEPATH=cppcheck ..
更多 "daily life" 示例可能会让您在 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()
参考资料
- CMake Commit: Add properties to run cppcheck along with the compiler
<LANG>_CPPCHECK
target propertyThis 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.