Cppcheck是一個檢查c/c++代碼的靜態代碼審查工具,不像c/c++的編譯器和許多其它代碼分析工具一樣,它不是用來檢測代碼的語法錯誤。Cppcheck只檢測編譯器通常不能檢測到的一些bug。它的目標是使程序不存在錯誤。之前我也有使用過一個pc-lint的代碼審查工具,相對來說,Cppcheck在檢測的全面性沒有pc-lint好,但是它貴在使用方便簡單。像我這種沒有人來做代碼審查的公司只能靠自己通過這類方便有效的工具來提高自己代碼的的質量了。我也是看了官方介紹說明,下面我就照着上面對其做一個簡單的使用介紹。
一、工具下載和文檔:
Cppcheck 最新更新版本下載地址:http://cppcheck.sourceforge.net/
介紹說明文檔地址:http://cppcheck.sourceforge.net/manual.html#idp287040
二、簡單使用說明:
三、錯誤類型嚴重級別分類:
# enable warning messages
cppcheck --enable=warning file.c
# enable performance messages
cppcheck --enable=performance file.c
# enable information messages
cppcheck --enable=information file.c
# For historical reasons, --enable=style enables warning, performance,
# portability and style messages. These are all reported as "style" when
# using the old xml format.
cppcheck --enable=style file.c
# enable warning and information messages
cppcheck --enable=warning,information file.c
# enable unusedFunction checking. This is not enabled by --enable=style
# because it doesn't work well on libraries.
cppcheck --enable=unusedFunction file.c
# enable all messages
cppcheck --enable=all
使用相關的 enable 消息可以只輸出指定的需要的信息,--enable = all是所有類型數據都打印輸出。四、將輸出結果寫到一個指定的文件中:
五、格式化輸出內容格式:
六、結合vs2010使用:
cppcheck --enable=all --template=vs %1 > NUL
因爲只需要cerr輸出,所以cout輸出到NUL.
然後在 VS->工具->外部工具 中添加:
標題:cppcheck
命令:d:\Cppcheck\vscppcheck.bat
參數:$(SolutionDir)
使用輸出窗口
設好後只需點工具cppcheck菜單,就會輸出代碼檢查結果,按<F4>可跳轉到錯誤行。