首先從這裏下載linux版本的:http://sourceforge.net/projects/cppcheck/files/cppcheck/
然後下載對應的版本,解壓,之後安裝:
編譯:
g++ -o cppcheck -Ilib cli/*.cpp lib/*.cpp
安裝:
make install
cppcheck默認安裝會報錯如下:
cppcheck: unusedFunction check can't be used with '-j' option. Disabling unusedFunction check.
(information) Failed to load std.cfg. Your Cppcheck installation is broken, please re-install. The Cppcheck binary was compiled without CFGDIR set. Either the std.cfg should be available in cfg or the CFGDIR should be configured.
因此我直接不用他們的make,而是直接使用g++參數編譯:g++ -o cppcheck -Ilib cli/*.cpp lib/*.cpp
然後就是檢查了
cppcheck -j 3 --enable=all src/
- 使能檢查規則:
- 默認:--enable=error
- --enable=all
- --enable=unusedFuntion path
- --enable=style
- 規則定義:
- error:出現的錯誤
- warning:爲了預防bug防禦性編程建議信息
- style:編碼格式問題(沒有使用的函數、多餘的代碼等)
- portablity:移植性警告。該部分如果移植到其他平臺上,可能出現兼容性問題
- performance:建議優化該部分代碼的性能
- information:一些有趣的信息,可以忽略不看的。
- 保存結果到文件中:重定向“>”
- -j 3 使用3個線程,如果代碼工程太大,可以使用15-20個,自己隨意發揮,不過還是參考cpuinfo
如果是Windows的版本呢,就這樣使用:
1、將Cppcheck綠色版cppcheck.rar解壓;
2、打開cppcheckgui.exe;
3、菜單 check->directory;
4、選擇源代碼目錄,確認;
5、Cppcheck自動開始走查;
轉載自:https://blog.csdn.net/pbymw8iwm/article/details/45310455