C + + code static Check tool Cppcheck installation configuration and use in the VS2008 development environment

Source: Internet
Author: User

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:

  1. #include "stdafx.h"
  2. #include <iostream>
  3. Using namespace std;
  4. int *p;
  5. int fun1 (int sz)
  6. {
  7. Delete [] p;
  8. //exception thrown in invalid state, ' p ' points at deallocated memory.
  9. if (sz <= 0)
  10. {
  11. throw Std::runtime_error ("size <= 0");
  12. }
  13. p = new Int[sz];
  14. }
  15. void *createfred ()
  16. {
  17. return malloc (100);
  18. }
  19. void destroyfred (void *p)
  20. {
  21. Free (p);
  22. }
  23. void f (int x)
  24. {
  25. //(style) Variable ' I ' is assigned a value , which is never used
  26. //(style) The scope of the variable I can be reduced
  27. int i;
  28. if (x = = 0)
  29. {
  30. i = 0;
  31. }
  32. }
  33. void foo (int x)
  34. {
  35. void *f = createfred ();
  36. if (x = = 1)
  37. {
  38. return;
  39. }
  40. //memory leak:f
  41. Destroyfred (f);
  42. }
  43. int _tmain (int argc, _tchar* argv[])
  44. {
  45. //error:array ' a[10] ' accessed at index ten, which is out of bounds.
  46. //variable ' A ' is assigned a value, which is never used.
  47. Char a[10];
  48. A[10] = 0;
  49. return 0;
  50. }

(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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.