最严格的 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用:
您应该使用
-O
以及-g
进行编译,因为某些警告仅在使用优化器时才可用(实际上,我通常使用>-O3
用于发现问题)。您可能更喜欢-std=gnu89
因为这会禁用库中的较少扩展。 OTOH,如果您按照严格的 ANSI C89 进行编码,也许您希望禁用它们。-ansi
选项与-std=c89
等效,但不那么明确或灵活。缺少的原型会警告您有关在范围内没有原型的情况下使用的函数(或定义的外部函数)的信息。严格的原型意味着您不能在函数声明或定义(或函数指针)中使用“空括号”;您需要
(void)
或正确的参数列表。旧样式定义具有 K&R 样式函数定义,例如:如果幸运的话,您无需担心这一点。我在工作中就没那么幸运了,而且我不能使用严格的原型,这让我很懊恼,因为周围有太多草率的函数指针。
另请参阅:什么是最好的命令 -用于清理代码的行工具
I'd recommend using:
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: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
这组选项非常好:
您必须阅读文档以查看该组合是否遗漏了任何额外的警告。
应该警告您,严格的 C89 不包括对
//
样式注释的支持,并且对具有外部链接的对象名称中的有效字符数有一些非常严格的限制。This set of options is pretty good:
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.