GCC的选择最严格的C代码? [英] GCC options for strictest C code?

查看:188
本文介绍了GCC的选择最严格的C代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCC选项应该设置为尽可能严格GCC? (并且我的意思是尽可能严格)我在C89中编写代码,并希望我的代码符合ANSI / ISO标准。

解决方案

我推荐使用:

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

您应该使用 -O 以及 -g ,因为一些警告仅在使用优化器时可用(实际上,我通常使用 -O3 来发现问题)。您可能更喜欢 -std = gnu89 ,因为这会禁用库中较少的扩展名。 OTOH,如果你编码严格的ANSI C89,也许你希望他们禁用。 -ansi 选项相当于 -std = c89 ,但不像显式或灵活。



缺少的原型会警告您在使用的函数(或定义的外部函数)时没有范围原型。严格的原型意味着你不能在函数声明或定义(或函数指针)中使用'空括号';你需要(void)或正确的参数列表。旧样式定义点K& R样式函数定义,如:

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

如果幸运的话,您不必担心这一点。我在工作中并不那么幸运,而且我也不能使用严格的原型,对我的懊恼来说,因为周围有太多松散的函数指针,所以我不能使用严格的原型。

另见:什么是最好的命令行工具清理代码


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.

解决方案

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

这篇关于GCC的选择最严格的C代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆