Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which g++ switches would help me write good code, like -Wall -Werror? [closed]

Tags:

c++

g++

I just got into C++ programming. Which commandline switches are a good idea to use in order to help me code?

I'm learning C++ by writing some small programs. I want my code to be as good as possible (at the expense of development and compilation time). For instance, switches that warn about portability problems, undefined behaviour and non-idomatic code would be useful.

I know of -Wall -Werror. Are there others?

like image 740
Andreas Avatar asked Oct 03 '13 05:10

Andreas


People also ask

Which switches are best for writing?

For the office, a tactile switch is more acceptable because they produce less noise compared to clicky switches. If you are in your own home, then a clicky switch will provide the tactility as well. The best three switches for typing are Cherry MX Browns, ZealPC Zilents, and Topre switches.

Are brown switches good for coding?

Brown switches are excellent for programming. The tactile bump helps limit mistakes which can increase the speed at which you enter data. Brown switches only produce a moderate amount of noise so they can be a good option for the office and you won't have to worry too much about how loud the keyboard is.

What color switches are best for typing?

The best switch ultimately comes down to personal preference. If you like the classic, clicky sound and feel, you'll love blue mechanical switches. If speed is a consideration, stick to linear (red) switches, and for a mix of both, go with brown.

What are yellow switches good for?

The fastest and quietest switch. Best for rapid-fire keypresses and fast-paced gaming, the Razer™ Yellow Mechanical Switch has an ultra-fast actuation of only 1.2mm, which allows you to press keys multiple times as fast as possible.


1 Answers

A list of gcc switches for code quality

  • -Wall for most common warnings
  • -Wextra for even more warnings which are still useful (this used to be just -W, if you see that switch mentioned it's same as this).
  • -Werror to force you to fix warnings by turning them into errors
  • -std=c++11 or whatever to specify a language standard or dialect (-ansi switch, which is sometimes used, is equal to some -std=X for each supported language, but I've seen different standards documented at different places...).
  • -pedantic or -pedantic-errors to tell gcc to be strict about the standard

Switches which I believe are not enabled by above (thanks for commenters!), and will help especially when learning, to help see what is happening implicitly. Read more in gcc doc for warning switches of gcc:

  • -Wconversion to force you to use explicit casts for example in cases where there is risk of value overflow and getting mangled value
  • -Wsign-conversion to force you to use explicit casts when there's risk of value getting mangled due to unsigned-signed conversion

However, gcc is not the ideal tool, or only tool you should use.

Use other tools than just compiler!

Very important tool is valgrind, which analyzes memory use of running program, and which you will want to use also in future, whenever you run into memory corruption problems. There are also GUIs for valgrind (check your Linux packages for easy installation), and latest versions of Qt Creator have pretty nice valgrind integration on Linux.

Then there are static code analyzers. Google for "C++ code analyzer" or "C++ lint" and also check the software repositiories of your Linux distribution (assuming you're using Linux, and if not, consider using one in a VM). One possiblity is C++ Lint, and another one is Clang Static Analyzer, though I have not tried either myself

Peer review

Nothing is as valuable for learning, as getting your code peer reviewed. However, SO is not the right place for plain review, but there's https://codereview.stackexchange.com/ which is in beta, and I think it is just the place to ask if learning code you have written is any good.

like image 175
hyde Avatar answered Sep 26 '22 15:09

hyde