安裝部署(Linux)
# 1. 上傳解壓
unzip cppcheck-1.61.zip
cd cppcheck-1.61
# 2. 賦予執行權限
chmod 777 cppcheck
# 3. 執行掃描(替換路徑)
./cppcheck --enable=all \
-I ~/rt0803/TGL/webkernel/foundations/include/ \
~/rt0803/TGL/webkernel/foundations/ \
2> result.txt
參數解析
| 參數 | 作用 |
--enable=all | 啟用所有檢查:錯誤、警告、風格、性能、可移植性、未使用函數等 |
-I path | 指定頭文件搜索路徑(可多次),解析 #include 依賴 |
源碼目錄 | 待掃描的源碼根目錄,遞歸處理 .c/.cpp/.h |
2> result.txt | stderr 重定向,捕獲所有輸出(含進度、錯誤) |
常用增強參數
# 指定 C++ 標準
--std=c++11
# 忽略特定檢查
--suppress=missingIncludeSystem
--suppress=unusedFunction
# 輸出 XML 供 CI 解析
--xml --xml-version=2 2> result.xml
# 多線程加速
-j 4
# 僅顯示錯誤級別
--error-exitcode=1 # 非 0 退出碼,適配 CI 門禁
結果解讀與修復流程
- 打開
result.txt,按文件/行號定位
- 關注
[error] [warning] [style] 標籤
- 典型問題:空指針解引用、緩衝區溢出、資源洩漏、未初始化變量、邏輯恆真/恆假
- 修復代碼 → 重新掃描 → 回歸驗證
CI/CD 集成建議