GCC options for strictest C code? [duplicate]

我只是一个虾纸丫 提交于 2019-12-03 08:35:26

问题


What GCC options should be set to have GCC as strict as possible? (and I do mean as strict as possible) I'm writing in C89 and want my code to be ANSI/ISO compliant.


回答1:


I'd recommend using:

-Wall -Wextra -std=c89 -pedantic -Wmissing-prototypes -Wstrict-prototypes \
    -Wold-style-definition

You should compile with -O as well as -g as some warnings are only available when the optimizer is used (actually, I usually use -O3 for spotting the problems). You might prefer -std=gnu89 as that disables fewer extensions in the libraries. OTOH, if you're coding to strict ANSI C89, maybe you want them disabled. The -ansi option is equivalent to -std=c89 but not quite as explicit or flexible.

The missing prototypes warns you about functions which are used (or external functions defined) without a prototype in scope. The strict prototypes means you can't use 'empty parentheses' for function declarations or definitions (or function pointers); you either need (void) or the correct argument list. The old style definition spots K&R style function definitions, such as:

int old_style(a, b) int a; double b; { ... }

If you're lucky, you won't need to worry about that. I'm not so lucky at work, and I can't use strict prototypes, much to my chagrin, because there are too many sloppy function pointers around.

See also: What is the best command-line tool to clean up code




回答2:


This set of options is pretty good:

-Wall -Wextra -ansi -pedantic

You'll have to read the documentation to see if there are any extra warnings getting left out by that combination.

You should be warned that strict C89 doesn't include support for // style comments, and there are some pretty serious restrictions on the number of significant characters in the names of objects with external linkage.



来源:https://stackoverflow.com/questions/8946797/gcc-options-for-strictest-c-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!