26

I am a newbie in C++ programming. When compiling I never use any option.

This is my day to day command:

g++ MyCode.cc -o MyCode

For safety practice what's the best option to use?

CTT
  • 16,045
  • 6
  • 40
  • 36
neversaint
  • 55,647
  • 127
  • 291
  • 457
  • Just a few results from the search: http://stackoverflow.com/questions/154630/recommended-gcc-warning-options-for-c http://stackoverflow.com/questions/399850/best-compiler-warning-level-for-c-c-compilers – Anonymous Feb 20 '09 at 08:20
  • 2
    You can achieve the same result by typing make – Martin York Feb 20 '09 at 09:08

10 Answers10

39
g++ -W -Wall -Werror

Will display all errors and warnings possible and treat them as errors.

LiraNuna
  • 61,525
  • 14
  • 115
  • 138
  • 7
    To the OP - Do what this poster suggests! You will save yourself many headaches if you just listen to the compiler warnings. Do not ignore them! – eduffy Feb 20 '09 at 14:44
  • 10
    Actually, it doesn't enable all possible warnings. -Wwrite-strings and -Wconversion are good too. – Steve Jessop Jul 13 '09 at 22:27
  • Another good one is `-Wnon-virtual-dtor` although it can result in false positives if the dtor is protected. – Mark B Mar 24 '11 at 16:06
  • 7
    Note: “`-W`” is the old name of `-Wextra`, and since it “adds” to `-Wall` I find the ordering “`-Wall -Wextra`” more logical. – gx_ Dec 04 '13 at 14:33
  • 1
    All possible warnings? [Heh.](http://stackoverflow.com/q/11714827/1858225) [Hehehehe.](http://stackoverflow.com/q/154630/1858225) [Hahahahaha!](http://stackoverflow.com/q/5088460/1858225) [Bwahahahahaha!](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=31573) – Kyle Strand Nov 04 '15 at 23:41
19

“-Werror”: treats all warnings as errors so you have to fix them. Incredibly valuable.

Bombe
  • 78,266
  • 20
  • 120
  • 125
19
g++ -g 

I really need that debug information....

18

If you thought you caught everything, try -Wextra

Dimitri Tcaciuc
  • 4,573
  • 3
  • 19
  • 22
11
g++ -Wall -Weffc++ -Werror -pedantic

When I'm using Boost, though, I drop it down to:

g++ -Wall -Werror

I'm anxiously awaiting GCC 4.4 and 4.5, though. There are some features coming that I really badly need.

greyfade
  • 24,214
  • 7
  • 64
  • 79
9
-ansi
-pedantic

-D__STDC_FORMAT_MACROS
-D__STDC_CONSTANT_MACROS
-D__STDC_LIMIT_MACROS
-D_GNU_SOURCE
-D_REENTRANT

-Wall
-Wextra
-Wwrite-strings
-Winit-self
-Wcast-align
-Wcast-qual
-Wold-style-cast
-Wpointer-arith
-Wstrict-aliasing
-Wformat=2
-Wuninitialized
-Wmissing-declarations
-Woverloaded-virtual
-Wnon-virtual-dtor
-Wctor-dtor-privacy
-Wno-long-long

-O3
-ftree-vectorize
-ftree-vectorizer-verbose=2
-ffast-math
-fstrict-aliasing
-march=native/pentium4/nocona/core2
-msse2
-mfpmath=sse
gx_
  • 4,500
  • 22
  • 31
Tom
  • 10,511
  • 3
  • 40
  • 49
8

We always use

g++ -Wall -Wextra ...
Brian Neal
  • 30,704
  • 7
  • 54
  • 59
5

Actually, it's a set: -Wall -pedantic -std=c++98

dirkgently
  • 104,737
  • 16
  • 128
  • 186
4

-pipe, it speeds up compilation a little bit. Also -O2, which speeds up execution.

Marc
  • 1,346
  • 2
  • 10
  • 15
  • 1
    Optimization will generally interfere with debugging. For a newbie, using the -g and without -Ox would be preferable. – Calyth Feb 20 '09 at 22:25
  • What speed improvements have you seen with -pipe? I find it doesn't make a significant difference, as the bulk of the time is spent linking (which can't be parallelized across multiple cores, like compilation). – Tom Mar 17 '09 at 13:02
3

I like -march=athlon -O2 -pipe for building most programs (I run Gentoo at home), and I use -ansi -pedantic -Wall for code I write myself.

Michael Kristofik
  • 33,068
  • 15
  • 76
  • 123