Recommended gcc warning options for C

Viewed 35124
15 Answers

I routinely use:

    gcc -m64 -std=c99 -pedantic -Wall -Wshadow -Wpointer-arith -Wcast-qual \
        -Wstrict-prototypes -Wmissing-prototypes

This set catches a lot for people unused to it (people whose code I get to compile with those flags for the first time); it seldom gives me a problem (though -Wcast-qual is occasionally a nuisance).

I started out with C++, so when I made the switch to learning C I made sure to be extra-anal:

-fmessage-length=0
-ansi -pedantic -std=c99
-Werror
-Wall
-Wextra
-Wwrite-strings
-Winit-self
-Wcast-align
-Wcast-qual
-Wpointer-arith
-Wstrict-aliasing
-Wformat=2
-Wmissing-declarations
-Wmissing-include-dirs
-Wno-unused-parameter
-Wuninitialized
-Wold-style-definition
-Wstrict-prototypes
-Wmissing-prototypes

I like -Werror. Keeps the code warning free.

I also use:

-Wstrict-overflow=5

To catch those nasty bugs that may occur if I write code that relies on the overflow behaviour of integers.

And:

-Wextra

Which enables some options that are nice to have as well. Most are for C++ though.

I usually compile with "-W -Wall -ansi -pedantic" this helps ensure maximum quality and portability of the code.

-pedantic -Wall -Wextra -Wno-write-strings -Wno-unused-parameter

For "Hurt me plenty" mode, I leave away the -Wno...

I like to have my code warning free, especially with C++. While C compiler warnings can often be ignored, many C++ warnings show fundamental defects in the source code.

Right now I use:

-Wall -W -Wextra -Wconversion -Wshadow -Wcast-qual -Wwrite-strings -Werror

I took that list mostly from the book "An introduction to gcc" and then some from Ulrich Drepper recomendation about Defensive Programming (http://people.redhat.com/drepper/Defensive-slides.pdf).

But I don't have any science behind my list, it just felt like a good list.

/Johan


Note: I don't like those pedantic flags though....

Note: I think that -W and -Wextra are more or less the same thing.

-Wfloat-equal, -Wshadow, -Wmissing-prototypes,

-Wredundant-decls -Wnested-externs -Wstrict-prototypes -Wextra -Werror-implicit-function-declaration -Wunused -Wno-unused-value -Wreturn-type

Related