C/C++代码静态检查工具Cppcheck在VS2008开发环境中的安装配置和使用
Cppcheck is an analysis tool for C/C++code. Unlike C/C++ compilers and many other analysis tools, it doesn’t detect syntax errors. Cppcheck only detects the types of bugs that the compilers normally fail to detect. The goal is no false positives.
Cppcheck is rarely wrong about reported errors. But there are many bugs that it doesn’t detect.
它可以检查不通过编译的文件。
执行的检查包括:
(1)、自动变量检查;(2)、数组的边界检查;(3)、class类检查;(4)、过期的函数,废弃函数调用检查;(5)、异常内存使用,释放检查;(6)、内存泄漏检查,主要是通过内存引用指针;(7)、操作系统资源释放检查,中断,文件描述符等;(8)、异常STL 函数使用检查;(9)、代码格式错误,以及性能因素检查。
安装步骤:
(1)、从http://sourceforge.net/projects/cppcheck/下载最新版本cppcheck-1.58-x86-Setup.msi,将其安装到D:\ProgramFiles\Cppcheck路径下(注意:不要包含中文路径,也可以从https://github.com/danmar/cppcheck/ 下载源代码);
(2)、打开vs2008,Tools-->ExternalTools-->点击Add,Title:Cppcheck;Command:D:\ProgramFiles\Cppcheck\cppcheck.exe;Argments:--quiet --verbose --template=vs$(ItemPath);Initial directory:$(ItemDir);选中Use Output window;点击OK.
例如,在F:\test\Cppcheck文件夹下创建了一个Cppcheck工程,F:\test\Cppcheck\Cppcheck文件夹下存放着一些.cpp文件:
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- int *p;
- int fun1(int sz)
- {
- delete [] p;
- //Exception thrown in invalid state, 'p' points at deallocated memory.
- if (sz <= 0)
- {
- throw std::runtime_error("size <= 0");
- }
- p = new int[sz];
- }
- void *CreateFred()
- {
- return malloc(100);
- }
- void DestroyFred(void *p)
- {
- free(p);
- }
- void f(int x)
- {
- //(style) Variable ’i’ is assigned a value that is never used
- //(style) The scope of the variable i can be reduced
- int i;
- if (x == 0)
- {
- i = 0;
- }
- }
- void foo(int x)
- {
- void *f = CreateFred();
- if (x == 1)
- {
- return;
- }
- //Memory leak: f
- DestroyFred(f);
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- //error: Array 'a[10]' accessed at index 10, which is out of bounds.
- //Variable 'a' is assigned a value that is never used.
- char a[10];
- a[10] = 0;
- return 0;
- }
(1)、checking all files in a folder:
D:\ProgramFiles\Cppcheck>cppcheckF:\test\Cppcheck\Cppcheck
(2)、stylistic issues(with --enable=style you enable most warning, styleand performance messages):
D:\ProgramFiles\Cppcheck>cppcheck--enable=style F:\test\Cppcheck\Cppcheck\Cppcheck.cpp
(3)、unused functions:
D:\ProgramFiles\Cppcheck>cppcheck--enable=unusedFunction F:\test\Cppcheck\Cppcheck
(4)、enable all checks:
D:\ProgramFiles\Cppcheck>cppcheck--enable=all F:\test\Cppcheck\Cppcheck
(5)、saving results in file:
D:\ProgramFiles\Cppcheck>cppcheck --enable=allF:\test\Cppcheck\Cppcheck 2> F:\test\Cppcheck\Cppcheck\err.txt
(6)、multithreaded checking(use 2 threads to check a folder):
D:\ProgramFiles\Cppcheck>cppcheck-j 2 F:\test\Cppcheck\Cppcheck
(7)、xml output:
D:\ProgramFiles\Cppcheck>cppcheck--xml-version=2 F:\test\Cppcheck\Cppcheck\Cppcheck.cpp
(8)、reformatting the output(to get Visual Studio compatible output):
D:\ProgramFiles\Cppcheck>cppcheck--template=vs F:\test\Cppcheck\Cppcheck\Cppcheck.cpp
参考文献:
1、http://sourceforge.net/apps/mediawiki/cppcheck/index.php?title=Main_Page
2、http://blog.csdn.net/akof1314/article/details/7477014
3、http://www.cppblog.com/jinq0123/archive/2012/04/10/170739.html
4、http://blog.sina.com.cn/s/blog_7a4cdec80100s661.html
5、http://avitebskiy.blogspot.tw/2012/10/poor-mans-visual-studio-cppcheck.html
代码检查工具列表:
1、http://en.wikibooks.org/wiki/Introduction_to_Software_Engineering/Tools/Static_Code_Analysis
2、http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis
3、http://www.cert.org/secure-coding/tools.html
5、http://www.kuqin.com/testing/20111116/314953.html
from:http://blog.csdn.net/fengbingchun/article/details/8887843
C/C++代码静态检查工具Cppcheck在VS2008开发环境中的安装配置和使用的更多相关文章
- 四种java代码静态检查工具
[转载]常用 Java 静态代码分析工具的分析与比较 转载自 开源中国社区 http://www.oschina.net/question/129540_23043 1月16日厦门 OSC ...
- Shell学习---Shell脚本的静态检查工具shellcheck
Shell脚本的静态检查工具shellcheck ubuntu下 apt install shellcheck ,即可安装shellcheck.写完shell脚本,记得用它检查一下,能给你点建议的.要 ...
- 静态代码检查工具 cppcheck 的使用
CppCheck是一个C/C++代码缺陷静态检查工具.不同于C/C++编译器及其它分析工具,CppCheck只检查编译器检查不出来的bug,不检查语法错误.所谓静态代码检查就是使用一个工具检查我们 ...
- 静态代码检查工具 cppcheck 的使用(可分别集成到VS和QT Creator里)
CppCheck是一个C/C++代码缺陷静态检查工具.不同于C/C++编译器及其它分析工具,CppCheck只检查编译器检查不出来的bug,不检查语法错误.所谓静态代码检查就是使用一个工具检查我们写的 ...
- Android(Java)利用findbugs进行代码静态检查
主要介绍利用java静态代码检查工具findbugs进行代码检查,包括其作用.安装.使用.高级功能(远程review和bug同步). 虽然Android提供了Test Project工程以及instr ...
- React Native工程中TSLint静态检查工具的探索之路
建立的代码规范没人遵守,项目中遍地风格迥异的代码,你会不会抓狂? 通过测试用例的程序还会出现Bug,而原因仅仅是自己犯下的低级错误,你会不会抓狂? 某种代码写法存在问题导致崩溃时,只能全工程检查代码, ...
- 玩转Eclipse — 自动代码规范检查工具Checkstyle
大项目都需要小组中的多人共同完成,但是每个人都有自己的编码习惯,甚至很多都是不正确的.那么如何使小组所有开发人员都遵循某些编码规范,以保证项目代码风格的一致性呢?如果硬性地要求每个开发人员在提交代码之 ...
- Kotlin Android项目静态检查工具的使用
Kotlin Android项目静态检查工具的使用 Kotlin Android项目可用的静态检查工具: Android官方的Lint, 第三方的ktlint和detekt. 静态检查工具 静态检查工 ...
- iOS工具】rvm、Ruby环境和CocoaPods安装使用及相关报错问题解决
〇.前言 <p>在iOS开发中 CocoaPods作为库依赖管理工具就是一把利器. 有了 CocoaPods则无需再通过拖 第三方库及第三方库所依赖的 framework静态库到项目中等麻 ...
随机推荐
- scrollTop,offset().top
1.scrollTop是指滚动条滚动的距离 如果没有出现滚动条,则距离为0 css: <style type="text/css"> *{ margin: 0; pad ...
- 论山寨手机与Android联姻的技术基础 【序】
山寨手机的兴起,离不开 MTK(联发科).MTK为手机制造提供了一揽子解决方案,其中既包括硬件,也包括软件.软件方面最重要的,是操作系统.MTK方案的软件的稳定性非常高,一方面是因为其硬件系统变化不大 ...
- Delphi使用XmlHttp获取时间
uses ComObj, DateUtils; procedure TForm1.Button1Click(Sender: TObject); var XmlHttp: Variant; datetx ...
- html和js
1.<input type="button" value="Hello world!"> 2.<button type="butto ...
- [置顶] hdu4747 Mex 线段树
题意:给你一个序列,让你求出对于所有区间<i, j>的mex和,mex表示该区间没有出现过的最小的整数. 思路:从时限和点数就可以看出是线段树,并且我们可以枚举左端点i, 然后求出所有左端 ...
- Android的logcat命令详解
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- Android UI ActionBar功能-自定义 Action Bar 样式
ActionBar的样式官方提供了三种: Theme.Holo Theme.Holo.Light Theme.Holo.Light.DarkActionBar 但不仅仅是这三种,我们还可以自己定义Ac ...
- Ognl中根元素与非根元素的关系
Ognl中根元素与非根元素的关系 根元素:可以理解为全局变量 非根元素:局部变量 从两者获取其属性的方式看: Object obj = Ognl.parseExpression(“[1]”); [1] ...
- iOS提交AppStore后申请加急审核(转)
是的,由于最近知名的Xcode后门事件,我们的应用也被感染了.o(╯□╰)o 上周四从看到喵神的微博得知第三方Xcode可能被感染后马上查了下,自己用的却是被感染了,于是马上到MAS下载了最新的Xco ...
- ICSharpCode.SharpZipLib.Zip
//压缩整个目录下载 var projectFolder = Request.Params["folder"] != null ? Request.Params["fol ...