Cppcheck is a analysis tool for C/c++code. Unlike C + + compilers and many other analysis tools, it doesn ' t detect syntax errors. Cppcheck only detects the types of bugs, the compilers normally fail to detect. The goal is no false positives.
Cppcheck is rarely wrong about reported errors. But there is many bugs that it doesn ' t detect.
It can check for files that are not compiled.
The checks performed include:
(1), automatic variable check, (2), array boundary check, (3), Class class Check, (4), expired function, discard function call check, (5), abnormal memory use, release check, (6), memory leak check, mainly through memory reference pointer; (7), operating system resource release check, Interrupt, file descriptor, etc. (8), exception STL function use Check, (9), code format error, and performance factor check.
Installation steps:
(1), download the latest version Cppcheck-1.58-x86-setup.msi from http://sourceforge.net/projects/cppcheck/, install it to D:\ProgramFiles\ Cppcheck Path (note: Do not include Chinese path, you can also download the source code from https://github.com/danmar/cppcheck/);
(2), open vs2008,tools-->externaltools--> click Add,title:cppcheck;command:d:\programfiles\cppcheck\ Cppcheck.exe;argments:--quiet--verbose--template=vs$ (itempath); Initial directory:$ (itemdir); Select Use Output window ; Click OK.
For example, a Cppcheck project was created under the F:\test\Cppcheck folder, and some. cpp files are stored under the F:\test\Cppcheck\Cppcheck folder:
- #include "stdafx.h"
- #include <iostream>
- Using namespace std;
- int *p;
- int fun1 (int sz)
- {
- Delete [] p;
- //exception thrown in invalid state, ' p ' points at deallocated memory.
- if (sz <= 0)
- {
- throw Std::runtime_error ("size <= 0");
- }
- p = new Int[sz];
- }
- void *createfred ()
- {
- return malloc (100);
- }
- void destroyfred (void *p)
- {
- Free (p);
- }
- void f (int x)
- {
- //(style) Variable ' I ' is assigned a value , which is never used
- //(style) The scope of the variable I can be reduced
- int i;
- if (x = = 0)
- {
- i = 0;
- }
- }
- void foo (int x)
- {
- void *f = createfred ();
- if (x = = 1)
- {
- return;
- }
- //memory leak:f
- Destroyfred (f);
- }
- int _tmain (int argc, _tchar* argv[])
- {
- //error:array ' a[10] ' accessed at index ten, which is out of bounds.
- //variable ' A ' is assigned a value, which is never used.
- Char a[10];
- A[10] = 0;
- return 0;
- }
(1), checking all files in a folder:
D:\programfiles\cppcheck>cppcheckf:\test\cppcheck\cppcheck
(2), stylistic issues (with--enable=style-enable most warning, Styleand performance messages):
D:\programfiles\cppcheck>cppcheck--enable=style F:\test\Cppcheck\Cppcheck\Cppcheck.cpp
(3), unused functions:
D:\programfiles\cppcheck>cppcheck--enable=unusedfunction F:\test\Cppcheck\Cppcheck
(4), enable all checks:
D:\programfiles\cppcheck>cppcheck--enable=all F:\test\Cppcheck\Cppcheck
(5), saving results in file:
D:\programfiles\cppcheck>cppcheck--enable=allf:\test\cppcheck\cppcheck 2> F:\test\Cppcheck\Cppcheck\ Err.txt
(6), multithreaded checking (use 2 threads to check a folder):
D:\programfiles\cppcheck>cppcheck-j 2 F:\test\Cppcheck\Cppcheck
(7), XML output:
d:\programfiles\cppcheck>cppcheck--xml-version=2 F:\test\Cppcheck\Cppcheck\Cppcheck.cpp
(8), reformatting the output (to get Visual Studio compatible output):
D:\programfiles\cppcheck>cppcheck--template=vs F:\test\Cppcheck\Cppcheck\Cppcheck.cpp
Reference documents:
1, Http://sourceforge.net/apps/mediawiki/cppcheck/index.php?title=Main_Page
2, http://blog.csdn.net/akof1314/article/details/7477014
3, http://www.cppblog.com/jinq0123/archive/2012/04/10/170739.html
4, http://blog.sina.com.cn/s/blog_7a4cdec80100s661.html
5, http://avitebskiy.blogspot.tw/2012/10/poor-mans-visual-studio-cppcheck.html
List of code checker tools:
1, Http://en.wikibooks.org/wiki/Introduction_to_Software_Engineering/Tools/Static_Code_Analysis
2, Http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis
3, http://www.cert.org/secure-coding/tools.html
4, http://spinroot.com/static/
5, http://www.kuqin.com/testing/20111116/314953.html
from:http://blog.csdn.net/fengbingchun/article/details/8887843
http://blog.csdn.net/flyingleo1981/article/details/51320575
C + + code static Check tool Cppcheck installation configuration and use in the VS2008 development environment