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

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

(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

目录
相关文章
|
30天前
|
C++
C++ 语言异常处理实战:在编程潮流中坚守稳定,开启代码可靠之旅
【8月更文挑战第22天】C++的异常处理机制是确保程序稳定的关键特性。它允许程序在遇到错误时优雅地响应而非直接崩溃。通过`throw`抛出异常,并用`catch`捕获处理,可使程序控制流跳转至错误处理代码。例如,在进行除法运算或文件读取时,若发生除数为零或文件无法打开等错误,则可通过抛出异常并在调用处捕获来妥善处理这些情况。恰当使用异常处理能显著提升程序的健壮性和维护性。
45 2
|
22天前
|
算法框架/工具 C++ Python
根据相机旋转矩阵求解三个轴的旋转角/欧拉角/姿态角 或 旋转矩阵与欧拉角(Euler Angles)之间的相互转换,以及python和C++代码实现
根据相机旋转矩阵求解三个轴的旋转角/欧拉角/姿态角 或 旋转矩阵与欧拉角(Euler Angles)之间的相互转换,以及python和C++代码实现
90 0
|
30天前
|
程序员 C++ 开发者
C++命名空间揭秘:一招解决全局冲突,让你的代码模块化战斗值飙升!
【8月更文挑战第22天】在C++中,命名空间是解决命名冲突的关键机制,它帮助开发者组织代码并提升可维护性。本文通过一个图形库开发案例,展示了如何利用命名空间避免圆形和矩形类间的命名冲突。通过定义和实现这些类,并在主函数中使用命名空间创建对象及调用方法,我们不仅解决了冲突问题,还提高了代码的模块化程度和组织结构。这为实际项目开发提供了宝贵的参考经验。
43 2
|
30天前
|
C++
拥抱C++面向对象编程,解锁软件开发新境界!从混乱到有序,你的代码也能成为高效能战士!
【8月更文挑战第22天】C++凭借其强大的面向对象编程(OOP)能力,在构建复杂软件系统时不可或缺。OOP通过封装数据和操作这些数据的方法于对象中,提升了代码的模块化、重用性和可扩展性。非OOP方式(过程化编程)下,数据与处理逻辑分离,导致维护困难。而OOP将学生信息及其操作整合到`Student`类中,增强代码的可读性和可维护性。通过示例对比,可以看出OOP使C++代码结构更清晰,特别是在大型项目中,能有效提高开发效率和软件质量。
21 1
|
23天前
|
C++
C++代码来计算一个点围绕另一个点旋转45度后的坐标
C++代码来计算一个点围绕另一个点旋转45度后的坐标
42 0
|
23天前
|
C++
Resharper c++ 使用Enter自动补全代码
Resharper c++ 使用Enter自动补全代码
28 0
|
30天前
|
监控 编译器 C++
【代码讲解】【C/C++】获取文件最后修改的时间(系统时间)
【代码讲解】【C/C++】获取文件最后修改的时间(系统时间)
34 0
|
2天前
|
编译器 C++
C++ 类构造函数初始化列表
构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式。
43 30
|
17天前
|
存储 编译器 C++
C ++初阶:类和对象(中)
C ++初阶:类和对象(中)
|
1月前
|
存储 安全 编译器
【C++】类和对象(下)
【C++】类和对象(下)
【C++】类和对象(下)