Recommended gcc warning options for C [closed]

2019-01-04 15:38发布

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

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

15条回答
beautiful°
2楼-- · 2019-01-04 16:03

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

查看更多
孤傲高冷的网名
3楼-- · 2019-01-04 16:08

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

查看更多
在下西门庆
4楼-- · 2019-01-04 16:14

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.

查看更多
放荡不羁爱自由
5楼-- · 2019-01-04 16:14

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
查看更多
ら.Afraid
6楼-- · 2019-01-04 16:15

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.

查看更多
别忘想泡老子
7楼-- · 2019-01-04 16:15

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
查看更多
登录 后发表回答