cpp-check
Skill8 starsChanged 16 days agoApache-2.0View on GitHub
使用 Cppcheck 对 C/C++ 代码做静态分析,检测缺陷、未定义行为、安全漏洞与风格问题。适用于 C/C++ 项目静态检查、CI 质量门禁、缺陷排查。
In this repository32 other files it gives its agents.
- AGENTS.mdAGENTS.md
- ast-grep-blockSkill
- code-deconstructSkill
- code-detect-dupSkill
- code-detect-problemSkill
- code-part-modificationSkill
- code-refactorSkill
- code-reviewSkill
- cpp-clang-tidySkill
- cpp-tscancodeSkill
- full-analysisSkill
- gdb-heap-analysisSkill
- java-asprofSkill
- java-check-pmdSkill
- java-check-spotbugsSkill
- java-coverageSkill
- java-envSkill
- java-g2mSkill
- jmh-benchSkill
- lets-loopSkill
- long-term-taskSkill
- md2pdfSkill
- merge-agents-mdSkill
- qualitative-assessmentSkill
- requirement-collectSkill
- rust-clippySkill
- rust-flamegraphSkill
- shell-checkSkill
- sql-extractSkill
- sync-dev-specSkill
- sync-repo-docsSkill
- vbox-test-envSkill
The file
Files
Cppcheck C/C++静态分析技能
概述
Cppcheck是C/C++静态分析工具,无需编译即可检测代码中的:
- 缺陷:空指针解引用、数组越界、内存泄漏
- 未定义行为:整数溢出、未初始化变量、空指针
- 安全漏洞:缓冲区溢出、格式化字符串漏洞
- 代码风格:冗余代码、复杂表达式
工具位置
CPPCHECK=/usr/bin/cppcheck
# 版本: Cppcheck 2.17.1执行流程
步骤1:确定源代码路径
SRC_DIR=src # 根据项目结构调整步骤2:执行分析
# 基础检查(默认级别)
$CPPCHECK --enable=warning $SRC_DIR
# 完整检查(含风格、性能、可移植性)
$CPPCHECK --enable=all $SRC_DIR 2> cppcheck-full.txt
# 仅缺陷检查(不含风格)
$CPPCHECK --enable=warning,information $SRC_DIR 2> cppcheck-defects.txt
# 安全漏洞专项
$CPPCHECK --enable=warning --check-level=exhaustive $SRC_DIR 2> cppcheck-security.txt步骤3:指定输出格式
# 文本格式(默认,输出到stderr)
$CPPCHECK --enable=all $SRC_DIR 2> cppcheck.txt
# XML格式
$CPPCHECK --enable=all --xml $SRC_DIR 2> cppcheck.xml
# XML版本2(推荐,用于CI集成)
$CPPCHECK --enable=all --xml-version=2 $SRC_DIR 2> cppcheck-v2.xml
# JSON格式
$CPPCHECK --enable=all --output-file=cppcheck.json --template=json $SRC_DIR常用参数
| 参数 | 描述 | 示例 |
--enable=<id> | 启用检查类别 | warning, style, performance, portability, information, all |
--check-level=<level> | 分析深度 | normal, exhaustive |
--std=<standard> | C/C++标准 | c11, c17, c++11, c++17, c++20 |
--platform=<type> | 目标平台 | unix32, unix64, win32, win64, native |
--language=<lang> | 强制语言 | c, c++ |
--suppress=<id> | 抑制特定警告 | unusedFunction, missingInclude |
--suppress=<id>:<file> | 抑制文件级警告 | unusedFunction:src/main.cpp |
--exit-on-suppression | 遇抑制项时退出 | 用于验证抑制列表 |
-I <dir> | 包含目录 | -I include -I /usr/include |
-D<macro> | 定义宏 | -DDEBUG -DNDEBUG |
-U<macro> | 取消宏定义 | -UDEBUG |
--output-file=<file> | 输出文件 | --output-file=report.txt |
--project=<file> | 项目文件 | --project=compile_commands.json |
--filter=<file> | 过滤文件 | --filter=cppcheck-filter.txt |
--addon=<name> | 加载插件 | --addon=misra --addon=cert |
--max-configs=<n> | 最大配置数 | --max-configs=20 |
--force | 强制检查所有配置 | 跳过配置限制 |
-j <n> | 并行检查线程数 | -j 4 |
检查类别详解
1. warning(默认启用)
| 检查项 | 描述 |
| 空指针解引用 | 解引用可能为NULL的指针 |
| 数组越界 | 数组索引超出范围 |
| 整数溢出 | 有符号整数运算溢出 |
| 未初始化变量 | 使用未初始化的变量 |
| 内存泄漏 | 动态分配的内存未释放 |
| 双重释放 | 同一内存释放两次 |
| 资源泄漏 | 文件句柄等资源未关闭 |
2. style
| 检查项 | 描述 |
| 冗余代码 | 永远不会执行的代码 |
| 复杂表达式 | 可简化的条件表达式 |
| 变量作用域 | 变量作用域可缩小 |
| 重复条件 | if/else分支条件重复 |
3. performance
| 检查项 | 描述 |
| 按值传递 | 大对象按值传递应改为引用 |
| 冗余拷贝 | 不必要的对象拷贝 |
| 低效算法 | 可优化的算法模式 |
4. portability
| 检查项 | 描述 |
| 平台依赖 | 依赖特定平台行为 |
| 类型大小 | 假设特定类型大小 |
| 未指定行为 | 依赖未指定的求值顺序 |
与构建系统集成
CMake集成
# 生成compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# CTest集成
add_test(NAME cppcheck
COMMAND /usr/bin/cppcheck --enable=all --xml-version=2
--project=${CMAKE_BINARY_DIR}/compile_commands.json
2>${CMAKE_BINARY_DIR}/cppcheck.xml)Makefile集成
cppcheck:
/usr/bin/cppcheck --enable=all --xml-version=2 \
-I include -I src \
src/ 2>cppcheck.xmlcompile_commands.json
# CMake自动生成
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
# Bear工具生成(非CMake项目)
bear -- make
# 使用compile_commands.json分析
$CPPCHECK --enable=all --project=compile_commands.json 2> cppcheck.xml抑制警告
行内抑制
// cppcheck-suppress memoryLeak
char* p = malloc(100);文件级抑制
$CPPCHECK --suppress=unusedFunction:src/main.cpp --enable=all $SRC_DIR抑制文件(suppressions.txt)
unusedFunction
memoryLeak:src/legacy.c
uninitvar:src/parser.c:100$CPPCHECK --suppressions-list=suppressions.txt --enable=all $SRC_DIRMISRA C/C++检查
# MISRA C 2012检查
$CPPCHECK --addon=misra --enable=style $SRC_DIR 2> misra.txt
# MISRA C++ 2008检查
$CPPCHECK --addon=misra-cpp2008 --enable=style $SRC_DIR 2> misra-cpp.txt分析结果处理
统计问题类型
grep -oP '\[(\w+)\]' cppcheck.txt | sort | uniq -c | sort -nr提取高危问题
grep -E '(error|warning):' cppcheck.txt生成摘要
echo "=== Cppcheck分析报告 ==="
echo "总问题数: $(grep -c '\[' cppcheck.txt)"
echo "错误: $(grep -c '(error)' cppcheck.txt)"
echo "警告: $(grep -c '(warning)' cppcheck.txt)"
echo "风格: $(grep -c '(style)' cppcheck.txt)"最佳实践
- 渐进式采用:先启用warning,逐步增加style、performance
- 使用compile_commands.json:确保头文件路径正确
- 并行分析:大型项目使用
-j参数加速 - 抑制管理:对误报使用抑制,定期审查抑制列表
- CI集成:将cppcheck纳入持续集成流程
技能版本: 1.0.0 工具版本: Cppcheck 2.17.1 适用项目: C/C++项目 输出位置: 当前目录下的cppcheck-*.txt文件
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

