我应该使用"-ansi"吗?或显式"-std = ..."作为编译器标志? [英] Should I use "-ansi" or explicit "-std=..." as compiler flags?

查看:181
本文介绍了我应该使用"-ansi"吗?或显式"-std = ..."作为编译器标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读 ANSI C与ISO C并不完全相同,并且编译器对"-ansi"的含义可能有所不同. (gcc将其映射到C90,将clang映射到C89)目前,我倾向于在"-ansi"上使用"-std = ...",因为这会明确显示所使用的标准.由于我对在Linux,Windows和MAC上进行编译特别感兴趣,因此我担心某些编译器无法理解"-std = ..."而不是"-ansi".那么,使用一个相对于另一个有什么利弊吗?

I've read that ANSI C is not exactly the same as ISO C and compilers may differ in interpretation of what "-ansi" is about. (gcc maps it to C90, clang maps it to C89) At the moment I would tend to use "-std=..." over "-ansi" as then it is explicitly shown which standard is used. As I am specifically interested in compiling on Linux, Windows and MAC, I fear some compilers could not understand "-std=..." but "-ansi". So are there any pros and cons for using the one over the other?

推荐答案

如果您希望编译器强制执行1989 ANSI C标准或等效的1990 ISO C标准(它们描述的语言完全相同),则可以放心使用-ansi-std=c89.

If you want the compiler to enforce the 1989 ANSI C standard, or equivalently the 1990 ISO C standard (they describe exactly the same language), you can safely use either -ansi or -std=c89.

严格来说,名称-ansi是错误的;它指的是1989年的ANSI C标准,但ANSI本身认为该标准已过时;它被1999 ISO C标准(ANSI在发布后不久正式采用)取代,该标准本身已经或即将被新的2011 ISO C标准取代.但是更改-ansi选项的含义将破坏太多的Makefile并构建脚本.

The name -ansi is, strictly speaking, incorrect; it refers to the 1989 ANSI C standard, but ANSI itself considers that standard to be obsolete; it was replaced by the 1999 ISO C standard (which ANSI officially adopted shortly after it was released) which itself either has been, or soon will be, replaced by the new 2011 ISO C standard. But changing the meaning of the -ansi option would break too many Makefiles and build scripts.

gcc 4.7和更高版本也将-std=c90识别为-std=c89的同义词. gcc 4.7于2012年3月发布,因此-std=c90是相当可移植的,除非您需要允许旧版本的gcc.

The gcc 4.7 and later versions also recognize -std=c90 as a synonym for -std=c89. gcc 4.7 was released in March 2012, so -std=c90 is reasonably portable unless you need to allow for older versions of gcc.

-std=c99强制执行(大部分)1999 ISO C标准.由于特别是Microsoft(即使在所有这些年之后)都不支持C99,因此使用此选项意味着编译器不会警告您有关其他地方可能不支持的C99特定功能的使用. gcc的C99支持在此处记录.

-std=c99 enforces (most of) the 1999 ISO C standard. Since Microsoft in particular doesn't support C99 (even after all these years), using this option means the compiler won't warn you about use of C99-specific features that might not be supported elsewhere. gcc's C99 support is documented here.

gcc 4.7对带有-std=c11的新ISO C 2011标准具有部分支持.该支持在以后的版本中有所改进,但尚未完成. gcc C11的状态为在此处记录,并且据说与C99支持级别相似.

gcc 4.7 has partial support for the new ISO C 2011 standard, with -std=c11. That support has improved in later releases, but is not yet complete. gcc C11 status is documented here, and is said to be similar to the level of C99 support.

还有更多选择,还有我提到的别名的许多别名;例如,选项-std=c9x是在1999年ISO标准最终确定之前添加的,并且仍受支持;同样,-std=c1x-std=c11的同义词.

There are more options, and a number of aliases for the ones I've mentioned; for example, the option -std=c9x was added before the 1999 ISO standard was finalized, and it's still supported; similarly, -std=c1x is a synonym for -std=c11.

我相信clang旨在与gcc尽可能兼容,因此它应该支持具有相同含义的相同选项(也许某些较新的选项除外,具体取决于您所使用的gcc和clang版本)使用).

I believe that clang is intended to be as compatible as possible with gcc, so it should support the same options with the same meanings (except perhaps for some of the newer ones, depending on which versions of gcc and clang you're using).

gcc手册具有完整的详细信息,其中一节描述了受支持的标准和另一个,用于指定各种-ansi-std=...选项.链接是到4.7版本的.您也可以运行info gcc(如果已安装GNU info命令和gcc文档),或者可以看到手册此处.

The gcc manual has the full details, with one section describing the supported standards and another specifying the various -ansi and -std=... options. The links are to the 4.7 version. You can also run info gcc (if you have the GNU info command and the gcc documentation installed), or you can see multiple versions of the manual here.

如果要使用gcc以外的其他编译器(以及旨在与gcc兼容的编译器),则必须阅读其文档以了解如何实施各种版本的C标准.

If you're going to use compilers other than gcc (and compilers that aim to be gcc-compatible), you'll have to read their documentation to find out how to enforce various versions of the C standard.

这篇关于我应该使用"-ansi"吗?或显式"-std = ..."作为编译器标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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