Cppcheck support in CMake

I am not asking about the various available third-party modules that support Cppcheck anyway.

With CMake 3.10, CMake seems to have received some official support from Cppcheck. See _CPPCHECK rel = "nofollow noreferrer"> CMAKE_ <LANG> _CPPCHECK .

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

+10
source share
1 answer

A simple example could be - if you have cppcheckin yours PATHand you do not specify additional parameters - the following, setting the 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 cppcheck Linux):

# 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_CXX_CPPCHECK cmake:

# cmake -DCMAKE_CXX_CPPCHECK:FILEPATH=cppcheck ..

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

+13

Source: https://habr.com/ru/post/1693198/


All Articles