如何在 VS 代码上编译和集成 Cppcheck 与 CMake?

问题描述

我是第一次为我的 C++ 项目设置构建系统。我正在尝试将 Cppcheck 静态分析工具与 CMake 构建系统生成器集成。

我尝试设置一些东西,但这些似乎对我不起作用。

请在下面找到:

代码:

#include <iostream>
int main(int argc,char **argv)
{
    std::cout << "Hello,Stackoverflow!" << std::endl;
    char a[10];
    a[10] = 0;
    std::cin.get();
    return 0;
}

Cppcheck.cmake

if(ENABLE_CPPCHECK)
  find_program(CPPCHECK cppcheck)
  if(CPPCHECK)
    message("Path: ${CPPCHECK}")

    set(CPPCHECK_TEMPLATE "cppcheck:{file}:{line}: {severity}:{message}")

    # set(CMAKE_CXX_CPPCHECK ${CPPCHECK}
    #   --enable=all
    #   --inconclusive
    #   --force
    #   --inline-suppr
    #   --template=${CPPCHECK_TEMPLATE}
    #   --verbose
    #   -i ${CMAKE_CURRENT_SOURCE_DIR}/src/
    # )

    add_custom_target(cppcheck
      COMMAND ${CPPCHECK}
        --enable=all
        --inconclusive
        --force
        --inline-suppr
        --template=${CPPCHECK_TEMPLATE}
        --verbose
      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/
      COMMENT "Checking ..."
      VERBATIM
    )

    message("debug value : ${CMAKE_CXX_CPPCHECK}")

  else()
    message(SEND_ERROR "cppcheck requested but executable not found")
  endif()
endif()

build> cmake .. 结果

-- Building for: Visual Studio 14 2015
-- Selecting Windows SDK version  to target Windows 10.0.17763.
-- The CXX compiler identification is MSVC 19.0.24215.1
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
Default build type: Debug
Path: C:/Program Files/Cppcheck/cppcheck.exe
debug value : C:/Program Files/Cppcheck/cppcheck.exe;--enable=all;--inconclusive;--force;--inline-suppr;--template=cppcheck:{file}:{line}: {severity}:{message};--verbose;-i;C:/dev/my-project/src/
-- Configuring done
-- Generating done
-- Build files have been written to: C:/dev/my-project/build

build> cmake --build . 结果

PS C:\dev\my-project\build> cmake --build .
Microsoft (R) Build Engine version 14.0.25420.1
Copyright (C) Microsoft Corporation. All rights reserved.

  Checking Build System
  Building Custom Rule C:/dev/my-project/src/CMakeLists.txt
  start.cpp
  start.vcxproj -> C:\dev\my-project\build\bin\Debug\start.exe
  start.vcxproj -> C:/dev/my-project/build/bin/Debug/start.pdb (Full PDB)
  Building Custom Rule C:/dev/my-project/CMakeLists.txt
PS C:\dev\my-project\build> .\bin\Debug\start.exe
Hello,Stackoverflow!

我在 Windows 上使用 Visual Studio 代码。 CMake 版本是 3.18。

我期待在这里从终端上的 Cppcheck 收到类似 [arrayIndexOutOfBounds] 的警告。 请让我知道这里出了什么问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...