C++ code analysis tools

后端 未结 8 2041
误落风尘
误落风尘 2021-02-06 07:49

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 f

相关标签:
8条回答
  • 2021-02-06 08:15

    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.

    0 讨论(0)
  • 2021-02-06 08:22

    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.

    0 讨论(0)
  • 2021-02-06 08:24

    I think you'd better have some lectures about good practices and why they are good. That should help you more than a code analysis tool (in the beginning at least).

    I suggest you read the series of Effective C++ and **Effective STL books, at least. See alsot The Definitive C++ Book Guide and List

    0 讨论(0)
  • 2021-02-06 08:31

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

    0 讨论(0)
  • 2021-02-06 08:31

    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.

    0 讨论(0)
  • 2021-02-06 08:32

    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.

    0 讨论(0)
提交回复
热议问题