现在的位置: 首页 > 综合 > 正文

静态代码检查工具的使用(cppcheck)

2012年08月30日 ⁄ 综合 ⁄ 共 963字 ⁄ 字号 评论关闭

所谓静态代码检查就是使用一个工具检查我们写的代码是否安全和健壮,是否有隐藏的问题。

比如无意间写了这样的代码:

 

  1. <span style="font-size: 14px;">int n = 10; 
  2. char* buffer = newchar[n]; 
  3. buffer[n] = 0;</span> 
int n = 10;
char* buffer = new char[n];
buffer[n] = 0;

这完全是符合语法规范的,但是静态代码检查工具会提示此处会溢出。也就是说,它是一个更加严格的编译器。

 

 

使用比较广泛的静态代码检查工具有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 called Tools.ExternalCommand1
  • click OK.

抱歉!评论已关闭.