CppCheck是一個C/C++代碼缺陷靜態檢查工具。不同於C/C++編譯器及其它分析工具,CppCheck只檢查編譯器檢查不出來的bug,不檢查語法錯誤。所謂靜態代碼檢查就是使用一個工具檢查我們寫的代碼是否安全和健壯,是否有隱藏的問題。
比如無意間寫了這樣的代碼:
- int n = 10;
- char* buffer = new char[n];
- buffer[n] = 0;
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