Recommended gcc warning options for C

CGccCompiler Errors

C Problem Overview


Other than -Wall what other warnings have people found useful?

http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Warning-Options.html

C Solutions


Solution 1 - C

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).

Solution 2 - C

As of 2011-09-01, with gcc version 4.6.1

My current "development" alias

gcc -std=c89 -pedantic -Wall 
-Wno-missing-braces -Wextra -Wno-missing-field-initializers -Wformat=2
-Wswitch-default -Wswitch-enum -Wcast-align -Wpointer-arith
-Wbad-function-cast -Wstrict-overflow=5 -Wstrict-prototypes -Winline
-Wundef -Wnested-externs -Wcast-qual -Wshadow -Wunreachable-code
-Wlogical-op -Wfloat-equal -Wstrict-aliasing=2 -Wredundant-decls
-Wold-style-definition -Werror
-ggdb3
-O0
-fno-omit-frame-pointer -ffloat-store -fno-common -fstrict-aliasing
-lm

The "release" alias

gcc -std=c89 -pedantic -O3 -DNDEBUG -flto -lm


As of 2009-11-03

"development" alias

gcc -Wall -Wextra -Wformat=2 -Wswitch-default -Wcast-align -Wpointer-arith 
-Wbad-function-cast -Wstrict-prototypes -Winline -Wundef -Wnested-externs
-Wcast-qual -Wshadow -Wwrite-strings -Wconversion -Wunreachable-code
-Wstrict-aliasing=2 -ffloat-store -fno-common -fstrict-aliasing
-lm -std=c89 -pedantic -O0 -ggdb3 -pg --coverage

"release" alias

gcc -lm -std=c89 -pedantic -O3 -DNDEBUG --combine -fwhole-program -funroll-loops

Solution 3 - C

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

Solution 4 - C

I like -Werror. Keeps the code warning free.

Solution 5 - C

Get the manual for the GCC version you use, find all warning options available, and then deactivate only those for which you have a compelling reason to do so. (For example, non-modifiable third-party headers that would give you lots of warnings otherwise.) Document those reasons. (In the Makefile or wherever you set those options.) Review the settings at regular intervalls, and whenever you upgrade your compiler.

The compiler is your friend. Warnings are your friend. Give the compiler as much chance to tell you of potential problems as possible.

Solution 6 - C

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.

Solution 7 - C

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

Solution 8 - C

-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.

Solution 9 - C

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.

Solution 10 - C

-pedantic-errors

Solution 11 - C

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

Solution 12 - C

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

Solution 13 - C

I generally just use

gcc -Wall -W -Wunused-parameter -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wsign-compare -Wconversion -Wshadow -Wcast-align -Wparentheses -Wsequence-point -Wdeclaration-after-statement -Wundef -Wpointer-arith -Wnested-externs -Wredundant-decls -Werror -Wdisabled-optimization -pedantic -funit-at-a-time -o

Solution 14 - C

The warning about uninitialized variables doesn't work unless you specify -O, so I include that in my list:

-g -O -Wall -Werror -Wextra -pedantic -std=c99

Solution 15 - C

-Wfatal-errors

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSardView Question on Stackoverflow
Solution 1 - CJonathan LefflerView Answer on Stackoverflow
Solution 2 - CpmgView Answer on Stackoverflow
Solution 3 - CTomView Answer on Stackoverflow
Solution 4 - CJesperEView Answer on Stackoverflow
Solution 5 - CDevSolarView Answer on Stackoverflow
Solution 6 - CNils PipenbrinckView Answer on Stackoverflow
Solution 7 - CEvan TeranView Answer on Stackoverflow
Solution 8 - CThorsten79View Answer on Stackoverflow
Solution 9 - CJohanView Answer on Stackoverflow
Solution 10 - CTom RitterView Answer on Stackoverflow
Solution 11 - CMark BesseyView Answer on Stackoverflow
Solution 12 - CflorinView Answer on Stackoverflow
Solution 13 - CamaterasuView Answer on Stackoverflow
Solution 14 - CJosh LeeView Answer on Stackoverflow
Solution 15 - CAdrian PanasiukView Answer on Stackoverflow