CMake中的Cppcheck支持 [英] Cppcheck support in CMake

查看:790
本文介绍了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.

不幸的是,如何使用此变量的文档有点稀疏。是否有一个很好的示例说明应该如何在CMake 3.10(或更高版本)中使用Cppcheck?

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

推荐答案

一个简单示例将是-如果您有 cppcheck PATH 中,并且您没有指定其他参数-通过设置全局 CMAKE_< Lang> _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系统上为 gcc cppcheck ):

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 ,只需通过<$ c $设置 CMAKE_CXX_CPPCHECK 变量即可c> cmake 命令行:

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

更多日常生活示例可能会为您在<$ c中包含以下代码片段$ c> 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()

引用

  • CMake Commit: Add properties to run cppcheck along with the compiler
  • <LANG>_CPPCHECK target property


仅当< LANG> C CXX

指定包含以下内容的; -list: cppcheck 静态分析工具的命令行。 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_< LANG> _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支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆