1. 程式人生 > >[程式設計工具]CppCheck程式碼檢測工具

[程式設計工具]CppCheck程式碼檢測工具

我們在寫程式碼的時候通常會犯兩樣低階錯誤,這種低階錯誤和那種低階錯誤。在編譯的時候可能不會報錯,但是在程式執行的時候就會發現很奇怪的結果,莫名其妙,如果不仔細點查詢可能就讓我們焦頭爛額。
其實可能是一個簡單的指標沒有釋放的問題,亦或是一個賦值寫錯了的問題,而CppCheck這個工具可以為我們的靜態程式碼檢測語法錯誤和記憶體洩露等問題。
CppCheck簡介:

Cppcheck – A tool for static C/C++ code analysis

Overview

Cppcheck is an analysis tool for C/C++ code. Unlike C/C++ compilers and many other analysis tools, we don’t detect syntax errors. Cppcheck only detects the types of bugs that the compilers normally fail to detect. The goal is no false positives.

We recommend that you enable as many warnings as possible in your compiler.
If you use Visual C++: you should use warning level 4.
If you use GCC: take a look at Warning options – using GCC
If you use another compiler: look in the manual.

Supported platforms:

  • You can check non-standard code that includes various compiler extensions, inline assembly code, etc.
  • Cppcheck is supposed to be compilable by any C++ compiler which handles the latest C++ standard.
  • Cppcheck is supposed to work on any platform that has sufficient cpu and memory.

cppcheck的使用方法可能是程式碼工具中最簡單的了,說一下使用方法。
1.下載CppCheck   

2.下載完後是一個MSI的安裝包,安裝之後,安裝目錄下有兩個工具,一個是帶介面的程式(cppcheckgui.exe),一個是console程式(cppcheck.exe)

3.

cppcheckgui.exe的使用方法

點選工具欄的第一個圖示,是載入資料夾,也就是你的專案資料夾,載入完成後它會自動檢測所有可能的問題並列出來,然後選擇File->Save results to file,檢測結果如下圖
cppcheckgui

 cppcheck.exe的使用方法

通過命令列進入cppcheck的安裝目錄並輸入以下命令

cppcheck D:/Project/XXX(資料夾完整路徑)  2>error.txt

會將錯誤生成在安裝目錄下名為error.txt的檔案中,方便檢視,結果如下:

[D:\Studio\BKWin\BKWin\resource.h:1]: (error) The code contains characters that are unhandled. Neither unicode nor extended ASCII are supported. (line=1, character code=ff)
[D:\Studio\BKWin\BKWin\resource.h:1]: (error) The code contains characters that are unhandled. Neither unicode nor extended ASCII are supported. (line=1, character code=fe)

可以看到兩種方法檢測到的結果是一樣的。

cppcheck支援的編譯器有很多種,分別如下:

=========
Cppcheck
=========
About

The original name of this program is “C++check” but it was later changed to “cppcheck”.

Manual

A manual is available online:
http://cppcheck.sf.net/manual.pdf

Compiling

Any C++ compiler should work.

To build the GUI, you need Qt.

When building the command line tool, PCRE is normally used.
PCRE is optional.

There are multiple compilation choices:
* qmake – cross platform build tool
* Windows: Visual Studio
* Windows: Qt Creator + mingw
* gnu make
* g++

qmake
=====
You can use the gui/gui.pro file to build the GUI.
cd gui
qmake
make

Visual Studio
=============
Use the cppcheck.sln file. The rules are normally enabled.

To compile with rules (pcre dependency):
* the pcre dll is needed. it can be downloaded from:
http://cppcheck.sf.net/pcre-8.10-vs.zip

To compile without rules (no dependencies):
* remove the preprocessor define HAVE_RULES from the project
* remove the pcre.lib from the project

Qt Creator + mingw
==================
The PCRE dll is needed to build the CLI. It can be downloaded here:
http://software-download.name/pcre-library-windows/

gnu make
========
To build Cppcheck with rules (pcre dependency):
make HAVE_RULES=yes

To build Cppcheck without rules (no dependencies):
make

g++ (for experts)
=================
If you just want to build Cppcheck without dependencies then you can use this command:
g++ -o cppcheck -Ilib cli/*.cpp lib/*.cpp

If you want to use –rule and –rule-file then dependencies are needed:
g++ -o cppcheck -lpcre -DHAVE_RULES -Ilib -Iexternals cli/*.cpp lib/*.cpp externals/tinyxml/*.cpp
mingw
=====
make LDFLAGS=-lshlwapi

Cross compiling Win32 (CLI) version of Cppcheck in Linux

sudo apt-get install mingw32
make CXX=i586-mingw32msvc-g++ LDFLAGS=”-lshlwapi”
mv cppcheck cppcheck.exe

Webpage

http://cppcheck.sourceforge.net/

我們可以不用擔心寫出來的簡單錯誤了,用它很輕鬆就檢測出來了,非常容易使用,寫程式碼的朋友,可以去試試了。