微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Cppcheck 与 CMake 集成用于某些目标

如何解决Cppcheck 与 CMake 集成用于某些目标

我无法弄清楚的是,如果我有一个 CMake 项目,其子文件夹包含自己的 CMakeLists.txt 和自己的目标: 我想为特定目标而不是所有目标集成 Cppcheck。 那么在这种情况下,我需要在根 CMakeLists.txt 中包含哪些内容,以及子文件夹的 CMakeLists 会发生什么变化?

解决方法

为每个您想要检查的目标设置 <LANG>_CPPCHECK 目标属性,并从您不想使用 cppcheck 检查的每个目标中清除它。


以下 CMakeLists.txt 文件:

cmake_minimum_required (VERSION 3.11)
project(test)
file(WRITE a.c "int main() { int a[10]; a[10] = 0; }")
file(WRITE b.c "int main() { int b[10]; b[10] = 0; }")
file(WRITE c.c "int main() { int c[10]; c[10] = 0; }")
file(WRITE a.h "static inline void f() { int a[10]; a[10] = 0; }")
file(WRITE b.h "static inline void f() { int b[10]; b[10] = 0; }")
file(WRITE c.h "static inline void f() { int c[10]; c[10] = 0; }")
add_executable(a a.c a.h)
add_executable(b b.c b.h)
add_executable(c c.c c.h)
set(cppcheck
  cppcheck
  "--enable=warning"
  "--inconclusive"
  "--force" 
  "--inline-suppr"
)
set_target_properties(a PROPERTIES C_CPPCHECK ${cppcheck})
set_target_properties(b PROPERTIES C_CPPCHECK ${cppcheck})

结果只检查a.cb.c文件,没有检查c.c文件。

$ cmake -S. -B_build -GNinja ; cmake --build _build
-- The C compiler identification is GNU 11.1.0
-- The CXX compiler identification is GNU 11.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /dev/shm/.1000.home.tmp.dir/10/_build
[3/6] Building C object CMakeFiles/b.dir/b.c.o
Checking ../b.c ...
../b.c:1:26: error: Array 'b[10]' accessed at index 10,which is out of bounds. [arrayIndexOutOfBounds]
int main() { int b[10]; b[10] = 0; }
                         ^
[4/6] Building C object CMakeFiles/a.dir/a.c.o
Checking ../a.c ...
../a.c:1:26: error: Array 'a[10]' accessed at index 10,which is out of bounds. [arrayIndexOutOfBounds]
int main() { int a[10]; a[10] = 0; }
                         ^
[6/6] Linking C executable a

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。