找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 6317|回复: 0

[编程工具]CppCheck代码检测工具

[复制链接]
发表于 2012-2-29 15:01:24 | 显示全部楼层 |阅读模式
我们在写代码的时候通常会犯两样低级错误,这种低级错误和那种低级错误。在编译的时候可能不会报错,但是在程序运行的时候就会发现很奇怪的结果,莫名其妙,如果不仔细点查找可能就让我们焦头烂额。
其实可能是一个简单的指针没有释放的问题,亦或是一个赋值写错了的问题,而CppCheck这个工具可以为我们的静态代码检测语法错误和内存泄露等问题。
CppCheck简介:



Cppcheck – A tool for static C/C++ code analysisOverviewCppcheck 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   CppCheck下载地址
2.下载完后是一个MSI的安装包,安装之后,安装目录下有两个工具,一个是带界面的程序(cppcheckgui.exe),一个是console程序(cppcheck.exe)
3.
cppcheckgui.exe的使用方法
点击工具栏的第一个图标,是加载文件夹,也就是你的项目文件夹,加载完成后它会自动检测所有可能的问题并列出来,然后选择File->Save results to file,检测结果如下图

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/
我们可以不用担心写出来的简单错误了,用它很轻松就检测出来了,非常容易使用,写代码的朋友,可以去试试了。

您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-2 06:09 , Processed in 0.011986 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表