当前位置: 首页 > news >正文

【2013-12-12】C语言代码检测:cppcheck的使用

[历史归档]本文原发布于 cstriker1407.info 个人博客,内容为历史存档,仅供参考。
发布时间:2013-12-12| 标题:C语言代码检测:cppcheck的使用分类:编程 / C && C++ |标签:C&&C++·代码检测·cppcheck


C语言代码检测:cppcheck的使用

    • 官网地址:
    • cppcheck的使用:
      • 界面语言的设置:
      • 保存输出文件:
      • 直接检查文件或目录:
    • cppcheck的输出文件结果实例:
      • 输出XML1:
      • 输出XML2:
      • 输出TXT:
      • 输出表格:
    • 更多参考:

cppcheck是一个轻量的跨平台的C C++ 代码静态检查工具,使用也很简单,这里备份下它的使用方法:

官网地址:

【 http://cppcheck.sourceforge.net/ 】

下载链接如下图,可以下载windows安装包,cppcheck源码,以及eclipse插件:

cppcheck的下载和安装在windows上是非常简单的,直接【 next 】即可。

cppcheck的使用:

界面语言的设置:

可在【 编辑 - 首选项 - 语言 】里选择

保存输出文件:

可在【 文件 - 保存结果到文件 】里选择

直接检查文件或目录:

可在【 检查 - 文件/目录 】里选择

检查结束后如下图:

cppcheck的输出文件结果实例:

cppcheck有4种输出结果:

输出XML1:

<?xml version="1.0" encoding="utf-8"?><results><errormsg="The scope of the variable 'len' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced: void f(int x) { int i = 0; if (x) { // it's safe to move 'int i = 0;' here for (int n = 0; n&lt;10; ++n) { // it is possible but not safe to move 'int i = 0;' here do_something(&amp;i); } } } When you see this message it is always safe to reduce the variable scope 1 level."severity="style"id="variableScope"line="255"file="utilssrc\argparse.c"/><errormsg="The scope of the variable 'c' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced: void f(int x) { int i = 0; if (x) { // it's safe to move 'int i = 0;' here for (int n = 0; n&lt;10; ++n) { // it is possible but not safe to move 'int i = 0;' here do_something(&amp;i); } } } When you see this message it is always safe to reduce the variable scope 1 level."severity="style"id="variableScope"line="287"file="utilssrc\linked_list_base.c"/><errormsg="Variable 'r' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"line="280"file="utilssrc\queue_base.c"/><errormsg="Variable 'r' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"line="294"file="utilssrc\queue_base.c"/><errormsg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"line="48"file="utilssrc\queue_internal.c"/><errormsg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"line="49"file="utilssrc\queue_internal.c"/><errormsg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"line="52"file="utilssrc\queue_internal.c"/><errormsg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"line="55"file="utilssrc\queue_internal.c"/><errormsg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"line="57"file="utilssrc\queue_internal.c"/><errormsg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"line="60"file="utilssrc\queue_internal.c"/><errormsg="Memory leak: new_el"severity="error"id="memleak"line="162"file="utilssrc\queue_internal.c"/></results>

输出XML2:

<?xml version="1.0" encoding="utf-8"?><resultsversion="2"><cppcheckversion="1.62"/>-<errors>-<errorverbose="The scope of the variable 'len' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced: void f(int x) { int i = 0; if (x) { // it's safe to move 'int i = 0;' here for (int n = 0; n&lt;10; ++n) { // it is possible but not safe to move 'int i = 0;' here do_something(&amp;i); } } } When you see this message it is always safe to reduce the variable scope 1 level."msg="The scope of the variable 'len' can be reduced."severity="style"id="variableScope"><locationline="255"file="utilssrc\argparse.c"/></error>-<errorverbose="The scope of the variable 'c' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced: void f(int x) { int i = 0; if (x) { // it's safe to move 'int i = 0;' here for (int n = 0; n&lt;10; ++n) { // it is possible but not safe to move 'int i = 0;' here do_something(&amp;i); } } } When you see this message it is always safe to reduce the variable scope 1 level."msg="The scope of the variable 'c' can be reduced."severity="style"id="variableScope"><locationline="287"file="utilssrc\linked_list_base.c"/></error>-<errorverbose="Variable 'r' is reassigned a value before the old one has been used."msg="Variable 'r' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"><locationline="279"file="utilssrc\queue_base.c"/><locationline="280"file="utilssrc\queue_base.c"/></error>-<errorverbose="Variable 'r' is reassigned a value before the old one has been used."msg="Variable 'r' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"><locationline="293"file="utilssrc\queue_base.c"/><locationline="294"file="utilssrc\queue_base.c"/></error>-<errorverbose="Variable 'error' is reassigned a value before the old one has been used."msg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"><locationline="46"file="utilssrc\queue_internal.c"/><locationline="48"file="utilssrc\queue_internal.c"/></error>-<errorverbose="Variable 'error' is reassigned a value before the old one has been used."msg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"><locationline="48"file="utilssrc\queue_internal.c"/><locationline="49"file="utilssrc\queue_internal.c"/></error>-<errorverbose="Variable 'error' is reassigned a value before the old one has been used."msg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"><locationline="49"file="utilssrc\queue_internal.c"/><locationline="52"file="utilssrc\queue_internal.c"/></error>-<errorverbose="Variable 'error' is reassigned a value before the old one has been used."msg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"><locationline="52"file="utilssrc\queue_internal.c"/><locationline="55"file="utilssrc\queue_internal.c"/></error>-<errorverbose="Variable 'error' is reassigned a value before the old one has been used."msg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"><locationline="55"file="utilssrc\queue_internal.c"/><locationline="57"file="utilssrc\queue_internal.c"/></error>-<errorverbose="Variable 'error' is reassigned a value before the old one has been used."msg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"><locationline="57"file="utilssrc\queue_internal.c"/><locationline="60"file="utilssrc\queue_internal.c"/></error>-<errorverbose="Memory leak: new_el"msg="Memory leak: new_el"severity="error"id="memleak"><locationline="162"file="utilssrc\queue_internal.c"/></error></errors></results>

输出TXT:

: (style) The scope of the variable 'len' can be reduced. : (style) The scope of the variable 'c' can be reduced. -> : (performance) Variable 'r' is reassigned a value before the old one has been used. -> : (performance) Variable 'r' is reassigned a value before the old one has been used. -> : (performance) Variable 'error' is reassigned a value before the old one has been used. -> : (performance) Variable 'error' is reassigned a value before the old one has been used. -> : (performance) Variable 'error' is reassigned a value before the old one has been used. -> : (performance) Variable 'error' is reassigned a value before the old one has been used. -> : (performance) Variable 'error' is reassigned a value before the old one has been used. -> : (performance) Variable 'error' is reassigned a value before the old one has been used. : (error) Memory leak: new_el

输出表格:

更多参考:

【 http://blog.csdn.net/e5max/article/details/11489137 】

【 http://blog.csdn.net/akof1314/article/details/7477014 】

【 http://www.cnblogs.com/lancidie/archive/2013/04/13/3019505.html 】

【 http://www.ibm.com/developerworks/cn/java/j-codan/ 】

【 http://www.wuqifu.com/2013/05/07/c静态代码检查工具cppcheck/ 】

http://www.jsqmd.com/news/1190281/

相关文章:

  • SpringSecurity进阶小册:Java码农必备!
  • ChatGPT技术选型终极checklist(2024.06更新版):覆盖模型层/推理层/缓存层/审计层/降级层——仅限前500名技术负责人领取完整Excel版
  • 3步学会岛屿设计:从零开始创建你的梦想岛屿
  • 2026年拼接地垫源头厂家行业实力排行名录 - 奔跑123
  • 2026 年当下,赤水热门的古代场景复原制造商哪家好,穿越回唐朝?先别急着兴奋,这3个细节会让你瞬间社死 - 企业推荐官【认证】
  • 商丘市区及虞城县本地正规家装公司行业盘点 - 奔跑123
  • 深度学习求职通关秘籍:Deep-Learning-Interview-Book开源项目深度解析与实战使用指南
  • 积家中国官方专柜客户电话权威信息通告(2026年7月最新) - 积家官方售后服务中心
  • 《Java 100 天进阶之路》第60篇:并发编程面试压轴题(2026版)
  • COM线程模型解析:STA与MTA的核心原理与优化实践
  • AI模型能力门控(Capability Gating)技术原理与工程实践
  • YOLO11 改进 - 特征融合 | STFFM空间时间特征融合模块,强化时空互补、抑制噪声,助力小目标检测高效涨点
  • 启动速度优化:AppStartup框架与冷启动秒开实践(164)
  • 万国官方服务项目及价格查询|全新电话和完整维修地址权威信息通告(2026年7月最新) - 万国中国官方服务中心
  • 2026 年现阶段,苍溪口碑好的厨卫漏水查漏服务团队选型指南,装修漏水惊魂!这招教你轻松查明厨卫隐形水源 - 鉴选官
  • 从课程到就业,AI培训怎么选才不踩坑? - GrowUME
  • 2026西安初高中冲刺补习:家长藏在日常里的升学心事 - 奔跑123
  • 2026年文旅行业SEO/GEO优化公司哪家好?避坑指南和选型深度解读 - GEO优化
  • 如何自定义 UIScrollView 的减速系数
  • 国内旋转轴封生产厂家排行:工况适配与服务实测 - 奔跑123
  • 亨得利官方钟表服务中心|全部地址及热线电话权威信息公示(2026年7月更新) - 亨得利官方
  • 【AI写Dockerfile实战指南】:20年DevOps专家亲授5大避坑法则与3类生产级模板生成技巧
  • 华硕设备管理器黄色感叹号:从芯片组驱动到固件更新的全方位排查指南
  • C++运算符重载图文详解
  • M芯片Mac系统降级实战:从Ventura回退Monterey的完整避坑指南
  • 无货源代发软件小白选购指南:认准货源、时效、售后三大核心 - 抖掌柜
  • 2026年高端全屋定制品牌深度评测:金牌家居领跑,精工智造定义品质新高度 - 商业科技观察
  • 天梭中国官方专柜服务电话权威信息公示(2026年7月最新) - 天梭服务中心
  • 帝舵中国官方售后服务中心|官方电话及服务网点地址权威信息声明(2026年7月最新) - 帝舵中国官方服务中心
  • 2026为什么需要茶麸头皮护理?适用人群作用原理与完整流程深度解读 - GrowUME