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

來源:互聯網
上載者:User

標籤:

 

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

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

[cpp] view plaincopy 
  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

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.