0

I am setting the build system for my C++ project first time. I am trying to integrate the Cppcheck static analysis tool with the CMake build system generator.

I have tried to set a few things and those seem is not working for me.

Please find below:

Code:

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

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

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!

I am using Visual studio code on windows. The CMake version is 3.18.

I am expecting here to get a warning like [arrayIndexOutOfBounds] from the Cppcheck on the terminal. Please let me know what is going wrong here.

Avi
  • 310
  • 2
  • 4
  • 16
  • Do I need something else at the time of compile instead of ```cmake ..```? – Avi Dec 31 '20 at 20:49
  • Is there an issue with the path of the source file? – Avi Dec 31 '20 at 20:49
  • Or the issue with the compiler settings? – Avi Dec 31 '20 at 20:50
  • 1
    What "issues" you are talking about? You show only output of the **configuration** (`cmake` call) stage, which is successful. Do you have an issue when you **build** the project? If yes, show its output too. – Tsyvarev Dec 31 '20 at 23:09
  • Thanks, @Tsyvarev! I am expecting this warning ```start.cpp:7:4: error: Array 'a[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds] a[10] = 0; ^``` at the time of configuration that I am not getting. If I do separately ```cppcheck ``` then I am getting the expected warning. Could you see any issue in steps? – Avi Jan 01 '21 at 03:06
  • 1
    That is, your problem is that the custom target is not executed when you build the project, isn't it? It is true, because by default custom target is executed only when you build that **specific** target (or by dependency). If you want your custom target to be executed when the whole project is built, you need to add `ALL` keyword to `add_custom_target` call: `add_custom_target(cppcheck ALL COMMAND ...)`. – Tsyvarev Jan 01 '21 at 10:28
  • Thanks! That's worked. I am an absolute beginner. I am going to delete the question. – Avi Jan 01 '21 at 14:44

0 Answers0