本文介绍了CMake 中的 Cppcheck 支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是在询问以一种或另一种方式支持 Cppcheck 的各种可用的第三方模块.

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

在 CMake 3.10 中,CMake 似乎获得了一些官方的 Cppcheck 支持.请参阅 CMAKE_<LANG>_CPPCHECK.

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

不幸的是,关于如何使用这个变量的文档有点少.是否有一个很好的例子说明 Cppcheck 应该如何与 CMake 3.10(或更高版本)一起使用?

Unfortunately the documentation 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)?

推荐答案

简单示例 将是 - 如果您的 PATH 中有 cppcheck 并且您没有指定其他参数 - 通过设置全局 CMAKE__CPPCHECK 变量:

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")

要扫描的文件会自动添加到 cppcheck 命令行.所以上面的例子给出了以下输出(Linux系统上的gcccppcheck):

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

您可以在现有项目中尝试 cppcheck,只需通过 cmake 命令行设置 CMAKE_CXX_CPPCHECK 变量:

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 ..

一个更日常生活"的例子可能会让您在 CMakeList.txt 中包含类似以下代码片段的内容:

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()

参考资料

仅当 CCXX 时才支持此属性.

cppcheck 静态分析工具指定一个包含命令行的 ;-list.Makefile 生成器和 Ninja 生成器将与编译器一起运行 cppcheck 并报告任何问题.

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.

如果在创建目标时设置了该属性,则该属性由 CMAKE__CPPCHECK 变量的值初始化.

This property is initialized by the value of the CMAKE_<LANG>_CPPCHECK variable if it is set when a target is created.

这篇关于CMake 中的 Cppcheck 支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-10 15:39