C++ code analysis tools

2019-03-16 02:38发布

I'm currently in the process of learning C++, and because I'm still learning, I keep making mistakes.
With a language as permissive as C++, it often takes a long time to figure out exactly what's wrong -- because the compiler lets me get away with a lot. I realize that this flexibility is one of C++'s major strengths, but it makes it difficult to learn the basic language.
Is there some tool I can use to analyze my code and make suggestions based on best practices or just sensible coding? Preferably as an Eclipse plugin or linux application.

8条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-16 02:58

Enable maximum compiler warnings (that's the -Wall option if you're using the Gnu compiler).

'Lint' is the archetypical static analysis tool.

valgrind is a good run-time analyzer.

查看更多
时光不老,我们不散
3楼-- · 2019-03-16 03:00

I think that really what you need to learn here is how to debug outside of an IDE. This is a valuable skill in my opinion, since you will no longer require such a heavy toolset to develop software, and it will apply to the vast majority of languages you already know and will ever learn.

However, its a difficult one to get used to. You will have to write code just for debugging purposes, e.g. write checks after each line not yet debugged, to ensure that the result is as expected, or print the values to the console or in message boxes so that you can check them yourself. Its tedious but will enable you to pick up on your mistakes more easily, inside or outside of an IDE.

Download and try some of the free debugging tools like GDB too, they can help you to probe memory, etc, without having to write your own code.

查看更多
4楼-- · 2019-03-16 03:01

Tool support for C++ is quite bad compared to Java, C#, etc. because it does not have a context-free grammar. In fact, there are parts of the C++ grammar that are undecidable. Basically, that means that understanding C++ code at the syntactic level requires implementing pretty much a compiler front end with semantic analysis. C++ cannot be parsed into an AST independently of semantic analysis, and most code analysis tools in IDEs, etc. work at the AST level. This is part of the tradeoff you make in exchange for the flexibility and backwards compatibility of C++.

查看更多
在下西门庆
5楼-- · 2019-03-16 03:02

There is as list of static code analysis tools at wikipedia.

But warnings are generally good but one problem with enabling all warnings with pedantic and Wall is the number of warnings you might get from including headers that you have no control over, this can create a lot of noise. I prefer to compile my own software with all warnings enabled though. As I program in linux I usually do like this:

Put the external headers I need to include in a separate file, and in the beginning of that file before the includes put:

#pragma GCC system_header

And then include this file from your code. This enables you to see all warnings from your own code without it being drowned in warnings from external code. The downside is that it's a gcc specific solution, I am not aware of any platform independent solution to this.

查看更多
Anthone
6楼-- · 2019-03-16 03:06

lint - there are lots of versions but if you google for lint you should find one that works. The other thing to do is turn on your compiler warnings - if you are using gcc/g++ the option is -Wall.

You might find CppChecker helpful as a plugin for Eclipse that supports gcc/PC lint.

查看更多
地球回转人心会变
7楼-- · 2019-03-16 03:07

For g++, as well as turning on -Wall, turn on -pedantic too, and prepare to be suprised at the number of issues it finds!

查看更多
登录 后发表回答