27

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,901
  • 6
  • 41
  • 37
neversaint
  • 60,904
  • 137
  • 310
  • 477
  • 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

40
g++ -W -Wall -Werror

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

LiraNuna
  • 64,916
  • 15
  • 117
  • 140
  • 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
20

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

Bombe
  • 81,643
  • 20
  • 123
  • 127
20
g++ -g 

I really need that debug information....

19

If you thought you caught everything, try -Wextra

Dimitri Tcaciuc
  • 5,053
  • 5
  • 20
  • 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,948
  • 7
  • 64
  • 80
10
-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,690
  • 24
  • 31
Tom
  • 10,689
  • 4
  • 41
  • 50
8

We always use

g++ -Wall -Wextra ...
Brian Neal
  • 31,821
  • 7
  • 55
  • 59
5

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

dirkgently
  • 108,024
  • 16
  • 131
  • 187
4

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

Marc
  • 1,356
  • 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
  • 34,290
  • 15
  • 75
  • 125