views:

1873

answers:

10

Hi,

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?

+11  A: 

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

Bombe
Along with -Wall to turn on all the useful warnings, of course.
Greg Hewgill
+4  A: 

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

dirkgently
+15  A: 
g++ -W -Wall -Werror

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

LiraNuna
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
Actually, it doesn't enable all possible warnings. -Wwrite-strings and -Wconversion are good too.
Steve Jessop
+4  A: 
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
What are the 4.4/4.5 features you're waiting for?
Ned
Lambdas (which I'm hoping show up in 4.5), auto variables (4.4), strongly-typed enums (4.4), and, most importantly, atomic<>, which I'm desperately hoping gets done in 4.5.
greyfade
Unfortunately, -Weffc++ makes objects with internal pointers a nightmare.
Tom
I know. That's why I don't use it with Boost - just including a Boost header creates another 30-40 warnings.
greyfade
+9  A: 
g++ -g

I really need that debug information....

anon
+8  A: 

If you thought you caught everything, try -Wextra

Dimitri Tcaciuc
+3  A: 

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

Marc
Optimization will generally interfere with debugging. For a newbie, using the -g and without -Ox would be preferable.
Calyth
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
+2  A: 

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.

Kristo
+2  A: 

We always use

g++ -Wall -Wextra ...
Brian Neal
+1  A: 

-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

Tom