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

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

问题描述

应该设置哪些 GCC 选项以使 GCC 尽可能严格?(我的意思是尽可能严格)我用 C89 编写,希望我的代码符合 ANSI/ISO.

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.

推荐答案

我建议使用:

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

您应该使用 -O 以及 -g 进行编译,因为某些警告仅在使用优化器时才可用(实际上,我通常使用 -O3 用于发现问题).您可能更喜欢 -std=gnu89,因为它禁用了库中较少的扩展.OTOH,如果您按照严格的 ANSI C89 进行编码,也许您希望它们被禁用.-ansi 选项等效于 -std=c89,但没有那么明确或灵活.

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.

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

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.

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

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

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