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文件:

  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4. int *p;
  5. int fun1(int sz)
  6. {
  7. delete [] p;
  8. //Exception thrown in invalid state, 'p' points at deallocated memory.
  9. if (sz <= 0)
  10. {
  11. throw std::runtime_error("size <= 0");
  12. }
  13. p = new int[sz];
  14. }
  15. void *CreateFred()
  16. {
  17. return malloc(100);
  18. }
  19. void DestroyFred(void *p)
  20. {
  21. free(p);
  22. }
  23. void f(int x)
  24. {
  25. //(style) Variable ’i’ is assigned a value that is never used
  26. //(style) The scope of the variable i can be reduced
  27. int i;
  28. if (x == 0)
  29. {
  30. i = 0;
  31. }
  32. }
  33. void foo(int x)
  34. {
  35. void *f = CreateFred();
  36. if (x == 1)
  37. {
  38. return;
  39. }
  40. //Memory leak: f
  41. DestroyFred(f);
  42. }
  43. int _tmain(int argc, _TCHAR* argv[])
  44. {
  45. //error: Array 'a[10]' accessed at index 10, which is out of bounds.
  46. //Variable 'a' is assigned a value that is never used.
  47. char a[10];
  48. a[10] = 0;
  49. return 0;
  50. }

(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

4、http://spinroot.com/static/

5、http://www.kuqin.com/testing/20111116/314953.html

from:http://blog.csdn.net/fengbingchun/article/details/8887843

C/C++代码静态检查工具Cppcheck在VS2008开发环境中的安装配置和使用的更多相关文章

  1. 四种java代码静态检查工具

    [转载]常用 Java 静态代码分析工具的分析与比较 转载自 开源中国社区 http://www.oschina.net/question/129540_23043       1月16日厦门 OSC ...

  2. Shell学习---Shell脚本的静态检查工具shellcheck

    Shell脚本的静态检查工具shellcheck ubuntu下 apt install shellcheck ,即可安装shellcheck.写完shell脚本,记得用它检查一下,能给你点建议的.要 ...

  3. 静态代码检查工具 cppcheck 的使用

      CppCheck是一个C/C++代码缺陷静态检查工具.不同于C/C++编译器及其它分析工具,CppCheck只检查编译器检查不出来的bug,不检查语法错误.所谓静态代码检查就是使用一个工具检查我们 ...

  4. 静态代码检查工具 cppcheck 的使用(可分别集成到VS和QT Creator里)

    CppCheck是一个C/C++代码缺陷静态检查工具.不同于C/C++编译器及其它分析工具,CppCheck只检查编译器检查不出来的bug,不检查语法错误.所谓静态代码检查就是使用一个工具检查我们写的 ...

  5. Android(Java)利用findbugs进行代码静态检查

    主要介绍利用java静态代码检查工具findbugs进行代码检查,包括其作用.安装.使用.高级功能(远程review和bug同步). 虽然Android提供了Test Project工程以及instr ...

  6. React Native工程中TSLint静态检查工具的探索之路

    建立的代码规范没人遵守,项目中遍地风格迥异的代码,你会不会抓狂? 通过测试用例的程序还会出现Bug,而原因仅仅是自己犯下的低级错误,你会不会抓狂? 某种代码写法存在问题导致崩溃时,只能全工程检查代码, ...

  7. 玩转Eclipse — 自动代码规范检查工具Checkstyle

    大项目都需要小组中的多人共同完成,但是每个人都有自己的编码习惯,甚至很多都是不正确的.那么如何使小组所有开发人员都遵循某些编码规范,以保证项目代码风格的一致性呢?如果硬性地要求每个开发人员在提交代码之 ...

  8. Kotlin Android项目静态检查工具的使用

    Kotlin Android项目静态检查工具的使用 Kotlin Android项目可用的静态检查工具: Android官方的Lint, 第三方的ktlint和detekt. 静态检查工具 静态检查工 ...

  9. iOS工具】rvm、Ruby环境和CocoaPods安装使用及相关报错问题解决

    〇.前言 <p>在iOS开发中 CocoaPods作为库依赖管理工具就是一把利器. 有了 CocoaPods则无需再通过拖 第三方库及第三方库所依赖的 framework静态库到项目中等麻 ...

随机推荐

  1. hdu 5446 Unknown Treasure 中国剩余定理+lucas

    题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. ...

  2. chroot

    用途:更改命令的根目录. 语法:chroot Directory Command 描述: 注意:如果新根目录中的特殊文件具有与实际根目录不同的主要和次要设备号,则可能会覆盖文件系统. 只有具有 roo ...

  3. PostgreSql入门命令

    1 命令行登录数据库 有两种方式,一是直接在系统shell下执行psql命令:而是先进入psql环境,然后再连接数据库.下面分别给出实例: (1)直接登录 执行命令:psql -h 192.168.1 ...

  4. docker 创建新的镜像到私有仓库

    docker:/data# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bd6db4127a9e centos &q ...

  5. 利用 Windows Azure 实现“云优先”

    根据 IDC 的调查,云计算无疑为我们的合作伙伴提供了巨大的机会,预计到 2016 年,全球企业将在公共云服务上耗资 980 亿美元.在今天的休斯敦全球合作伙伴大会上,我们非常高兴能与合作伙伴共同寻求 ...

  6. Codeforces 286E

    #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...

  7. java 书籍推荐 JavaEE程序员必读图书大推荐

    java 书籍推荐 JavaEE程序员必读图书大推荐 转自:http://www.cnblogs.com/xlwmin/articles/2192775.html 下面是我根据多年的阅读和实践经验,给 ...

  8. poj 3250 Bad Hair Day(单调队列)

    题目链接:http://poj.org/problem?id=3250 思路分析:题目要求求每头牛看见的牛的数量之和,即求每头牛被看见的次数和:现在要求如何求出每头牛被看见的次数? 考虑到对于某头特定 ...

  9. string 到 wstring的转换

    string 到 wstring的转换_一景_新浪博客     string 到 wstring的转换    (2009-08-10 20:52:34)    转载▼    标签:    杂谈    ...

  10. o​r​a​l​c​e​ ​D​B​A​ ​培​训_lesson06

    控制文件 -小型二进制文件 -定义物理数据库的当前状态 -丢失控制文件须要修复 -维护数据库的完整性 -要求: 1.在启动数据库时处于mount状态 2.可以操作数据库 -仅仅链接至一个数据库 -最初 ...