您的位置:首页 > 编程语言 > C语言/C++

Cppcheck

2015-06-05 17:09 316 查看
from:http://blog.csdn.net/yzlworld/article/details/29572051

工具简介

下载安装(Linux):

官方网址:http://sourceforge.net/projects/cppcheck/
安装方式:解压后,直接make进行编译,完成后,make install进行安装
验证安装:cppcheck -v 查看是否安装成功

特点介绍

主要检查编译器通常不能发现的缺陷。
cppcheck能够发现很多错误,但不能发现所有的错误。

基本使用

检查单个文件:cppcheck foo.c
检查文件夹:cppcheck path
排除一个文件或者一个文件夹:cppcheck -isrc/foo.c src
使能检查规则:

默认:--enable=error
--enable=all
--enable=unusedFuntion path
--enable=style

规则定义:

error:出现的错误
warning:为了预防bug防御性编程建议信息
style:编码格式问题(没有使用的函数、多余的代码等)
portablity:移植性警告。该部分如果移植到其他平台上,可能出现兼容性问题
performance:建议优化该部分代码的性能
information:一些有趣的信息,可以忽略不看的。

保存结果到文件中:重定向“>”
多线程检查代码(提高检查速度,充分利用CPU功能):cppcheck -j 4 src

高级使用

xml 输出

使用方式:cppcheck --xml-version=2 foo.cpp
error的组成元素

id:error的id
severity:error的性质(error、warning......)
msg:error的具体信息(短格式)
verbose:error的信息(长格式)

location的组成元素:

file:含有相对或者绝对路径的文件名
line:行数
msg:消息描述

改良输出结果

cppcheck --template=vs path (Visual Studio 兼容模式)
cppcheck --template=gcc path (Gcc兼容模式)
cppcheck --template={"{file},{line},{severity},{id},{message}"} (自定义模式)

输出过滤器(选特定的错误信息输出)

命令行模式:cppcheck --suppress=memleak:src/file1.cpp src/
使用文件模式(将过滤规则存到文件中):cppcheck --suppressions suppressions.txt src/

报告美化

./cppcheck test.cpp --xml 2> err.xml

htmlreport/cppcheck-htmlreport --file=err.xml --report-dir=test1 --source-dir=.

GUI应用(cppcheck提供GUI的用户接口,比较简单,略去不写)
参考cppcheck用户手册
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: