静态代码检查工具的使用(cppcheck)
2016-03-24 14:47
274 查看
所谓静态代码检查就是使用一个工具检查我们写的代码是否安全和健壮,是否有隐藏的问题。
比如无意间写了这样的代码:
int n
= 10;
char*
buffer = newchar
;
buffer
= 0;</span>
这完全是符合语法规范的,但是静态代码检查工具会提示此处会溢出。也就是说,它是一个更加严格的编译器。
使用比较广泛的静态代码检查工具有cppcheck pc-lint等。
pc-lint是资格最老,最强力的代码检查工具,但是是收费软件,并且配置起来有一点点麻烦。
cppcheck是免费的开源软件。使用起来也很方便。
从 http://cppcheck.sourceforge.net/
下载最新的cppcheck。
安装完就可以使用里面的cppcheck-gui来检测代码。
参考这里(http://avitebskiy.blogspot.tw/2012/10/poor-mans-visual-studio-cppcheck.html),可以方便的把cppcheck嵌入到vs,然后可以方便的对一个文件进行检查,而且支持错误跳转。
click the Add button
set the Title, for example Cppcheck
set Command to C:\Program Files (x86)\Cppcheck\cppcheck.exe
set Arguments to --quiet --verbose --template=vs $(ItemPath)
set Initial Directory to $(ItemDir)
make sure Use Output window checkbox is enabled
click on the Move Up button repeatedly until your entry is at the top of the list, this will make it easier to identify you new command as you can count on it being calledTools.ExternalCommand1
click OK.
比如无意间写了这样的代码:
int n
= 10;
char*
buffer = newchar
;
buffer
= 0;</span>
<span style="font-size: 14px;">int n = 10; char* buffer = new char ; buffer = 0;</span>
这完全是符合语法规范的,但是静态代码检查工具会提示此处会溢出。也就是说,它是一个更加严格的编译器。
使用比较广泛的静态代码检查工具有cppcheck pc-lint等。
pc-lint是资格最老,最强力的代码检查工具,但是是收费软件,并且配置起来有一点点麻烦。
cppcheck是免费的开源软件。使用起来也很方便。
从 http://cppcheck.sourceforge.net/
下载最新的cppcheck。
安装完就可以使用里面的cppcheck-gui来检测代码。
参考这里(http://avitebskiy.blogspot.tw/2012/10/poor-mans-visual-studio-cppcheck.html),可以方便的把cppcheck嵌入到vs,然后可以方便的对一个文件进行检查,而且支持错误跳转。
click the Add button
set the Title, for example Cppcheck
set Command to C:\Program Files (x86)\Cppcheck\cppcheck.exe
set Arguments to --quiet --verbose --template=vs $(ItemPath)
set Initial Directory to $(ItemDir)
make sure Use Output window checkbox is enabled
click on the Move Up button repeatedly until your entry is at the top of the list, this will make it easier to identify you new command as you can count on it being calledTools.ExternalCommand1
click OK.
相关文章推荐
- Cppcheck 1.54 C/C++静态代码分析工具
- C++用钩子模仿按键精灵录制键盘鼠标的功能
- C++ 空指针调用函数 静态绑定
- P2P通讯的C++实现-UDP Hole Punching方法
- C语言文件读写操作详解
- C++的类定义中,不能在类中定义自身类的对象,java中却可以
- #if defined和#if !defined(c语言的宏定义)
- C++ 类的继承和组合
- 深入理解C指针学习笔记(三)
- C++ const提高函数的健壮性
- c++实验2——项目1
- mex error: unrecognized command line option ‘-std=c++11’
- [c++][语言语法]stringstream iostream ifstream
- C++实验2-标准体重
- c++第二次实验
- C语言单向链表的建立
- C语言中堆和栈的区别
- leetcode_326_Power of Three(easy)(C++)
- 平衡二叉树C++模板
- C语言文件读写函数详解