靜態代碼檢查工具 cppcheck 的使用


 

      CppCheck是一個C/C++代碼缺陷靜態檢查工具。不同於C/C++編譯器及其它分析工具,CppCheck只檢查編譯器檢查不出來的bug,不檢查語法錯誤。所謂靜態代碼檢查就是使用一個工具檢查我們寫的代碼是否安全和健壯,是否有隱藏的問題。

比如無意間寫了這樣的代碼: 

[cpp]  view plain copy
 
  1. int n = 10;  
  2. char* buffer = new char[n];  
  3. buffer[n] = 0;  

 

這完全是符合語法規范的,但是靜態代碼檢查工具會提示此處會溢出。也就是說,它是一個更加嚴格的編譯器。使用比較廣泛的C/C++靜態代碼檢查工具有cppcheck    pc-lint等。pc-lint是資格最老,最強力的代碼檢查工具,但是是收費軟件,並且配置起來有一點點麻煩。

CppCheck 是免費的開源軟件。使用起來也很方便。 

從 http://cppcheck.sourceforge.net/   下載最新的cppcheck。

使用方式:

一、GUI方式:安裝完就可以使用里面的cppcheck-gui來檢測代碼。界面如下:

 

二、命令行方式:

 

三、集成到IDE開發環境中使用:

1、VS

參考這里(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.

2、Qt Creator

     在QtCreator中點擊:tools=>external=>config...=>add 彈出如下對話框:

 

填入下列參數:

設置完后就可以用cppcheck檢查指定目錄下的代碼文件:tools=>external=>cppcheck 開始檢查。

參考文章: http://www.cnblogs.com/lancidie/archive/2013/04/13/3019505.html

以下這篇文章也非常詳細:http://blog.csdn.net/akof1314/article/details/7477014


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM