GCC 的 C 标准探索

测试交叉工具链:

$ arm-linux-gcc -std=gnu17 a.c -o a -v

 1 使用内建 specs。
 2 COLLECT_GCC=/home/sun/18.04-install/toolchain/bin/arm-linux-gcc.br_real
 3 COLLECT_LTO_WRAPPER=/home/sun/18.04-install/toolchain/bin/../libexec/gcc/arm-buildroot-linux-gnueabi/8.2.0/lto-wrapper
 4 目标:arm-buildroot-linux-gnueabi
 5 配置为:./configure --prefix=/home/sun/share/build/buildroot-2018.08.2/output/host --sysconfdir=/home/sun/share/build/buildroot-2018.08.2/output/host/etc --enable-static --target=arm-buildroot-linux-gnueabi --with-sysroot=/home/sun/share/build/buildroot-2018.08.2/output/host/arm-buildroot-linux-gnueabi/sysroot --disable-__cxa_atexit --with-gnu-ld --disable-libssp --disable-multilib --with-gmp=/home/sun/share/build/buildroot-2018.08.2/output/host --with-mpc=/home/sun/share/build/buildroot-2018.08.2/output/host --with-mpfr=/home/sun/share/build/buildroot-2018.08.2/output/host --with-pkgversion='Buildroot 2018.08.2' --with-bugurl=http://bugs.buildroot.net/ --disable-libquadmath --enable-tls --enable-plugins --enable-lto --disable-libmudflap --enable-threads --without-isl --without-cloog --with-float=soft --disable-decimal-float --with-abi=aapcs-linux --with-cpu=cortex-a15 --with-float=soft --with-mode=arm --enable-languages=c,c++ --with-build-time-tools=/home/sun/share/build/buildroot-2018.08.2/output/host/arm-buildroot-linux-gnueabi/bin --enable-shared --enable-libgomp
 6 线程模型:posix
 7 gcc 版本 8.2.0 (Buildroot 2018.08.2) 
 8 COLLECT_GCC_OPTIONS='-std=gnu17' '-o' 'a' '-v' '-mcpu=cortex-a15' '-mfloat-abi=soft' '-mabi=aapcs-linux' '-marm' '-mtls-dialect=gnu' '-march=armv7ve'
 9  /home/sun/18.04-install/toolchain/bin/../libexec/gcc/arm-buildroot-linux-gnueabi/8.2.0/cc1 -quiet -v -iprefix /home/sun/18.04-install/toolchain/bin/../lib/gcc/arm-buildroot-linux-gnueabi/8.2.0/ -isysroot /home/sun/18.04-install/toolchain/arm-buildroot-linux-gnueabi/sysroot a.c -quiet -dumpbase a.c -mcpu=cortex-a15 -mfloat-abi=soft -mabi=aapcs-linux -marm -mtls-dialect=gnu -march=armv7ve -auxbase a -std=gnu17 -version -o /tmp/ccjATSqw.s
10 GNU C17 (Buildroot 2018.08.2) 版本 8.2.0 (arm-buildroot-linux-gnueabi)
11     由 GNU C 版本 7.3.0 编译,GMP 版本 6.1.2,MPFR 版本 3.1.6,MPC 版本 1.0.3,isl 版本 none
12 GGC 准则:--param ggc-min-expand=100 --param ggc-min-heapsize=131072
13 忽略重复的目录“/home/sun/18.04-install/toolchain/bin/../lib/gcc/../../lib/gcc/arm-buildroot-linux-gnueabi/8.2.0/include”
14 忽略不存在的目录“/home/sun/18.04-install/toolchain/arm-buildroot-linux-gnueabi/sysroot/usr/local/include”
15 忽略重复的目录“/home/sun/18.04-install/toolchain/bin/../lib/gcc/../../lib/gcc/arm-buildroot-linux-gnueabi/8.2.0/include-fixed”
16 忽略重复的目录“/home/sun/18.04-install/toolchain/bin/../lib/gcc/../../lib/gcc/arm-buildroot-linux-gnueabi/8.2.0/../../../../arm-buildroot-linux-gnueabi/include”
17 #include "..." 搜索从这里开始:
18 #include <...> 搜索从这里开始:
19  /home/sun/18.04-install/toolchain/bin/../lib/gcc/arm-buildroot-linux-gnueabi/8.2.0/include
20  /home/sun/18.04-install/toolchain/bin/../lib/gcc/arm-buildroot-linux-gnueabi/8.2.0/include-fixed
21  /home/sun/18.04-install/toolchain/bin/../lib/gcc/arm-buildroot-linux-gnueabi/8.2.0/../../../../arm-buildroot-linux-gnueabi/include
22  /home/sun/18.04-install/toolchain/arm-buildroot-linux-gnueabi/sysroot/usr/include
23 搜索列表结束。
24 GNU C17 (Buildroot 2018.08.2) 版本 8.2.0 (arm-buildroot-linux-gnueabi)
25     由 GNU C 版本 7.3.0 编译,GMP 版本 6.1.2,MPFR 版本 3.1.6,MPC 版本 1.0.3,isl 版本 none
26 GGC 准则:--param ggc-min-expand=100 --param ggc-min-heapsize=131072
27 Compiler executable checksum: 554fb17466f5d4550a145bf06addc691
28 a.c: 在函数‘audio_sem_wait’中:
29 a.c:140:10: 警告:implicit declaration of function ‘semtimedop’; did you mean ‘semop’? [-Wimplicit-function-declaration]
30    return semtimedop(sem_id, &sem_ops, 1, NULL);
31           ^~~~~~~~~~
32           semop
33 COLLECT_GCC_OPTIONS='-std=gnu17' '-o' 'a' '-v' '-mcpu=cortex-a15' '-mfloat-abi=soft' '-mabi=aapcs-linux' '-marm' '-mtls-dialect=gnu' '-march=armv7ve'
34  /home/sun/18.04-install/toolchain/bin/../lib/gcc/arm-buildroot-linux-gnueabi/8.2.0/../../../../arm-buildroot-linux-gnueabi/bin/as -v -march=armv7ve -mfloat-abi=soft -meabi=5 -o /tmp/cch1eWdG.o /tmp/ccjATSqw.s
35 GNU汇编版本 2.31.1 (arm-buildroot-linux-gnueabi) 使用BFD版本 (GNU Binutils) 2.31.1
36 COMPILER_PATH=/home/sun/18.04-install/toolchain/bin/../libexec/gcc/arm-buildroot-linux-gnueabi/8.2.0/:/home/sun/18.04-install/toolchain/bin/../libexec/gcc/:/home/sun/18.04-install/toolchain/bin/../lib/gcc/arm-buildroot-linux-gnueabi/8.2.0/../../../../arm-buildroot-linux-gnueabi/bin/
37 LIBRARY_PATH=/home/sun/18.04-install/toolchain/bin/../lib/gcc/arm-buildroot-linux-gnueabi/8.2.0/:/home/sun/18.04-install/toolchain/bin/../lib/gcc/:/home/sun/18.04-install/toolchain/bin/../lib/gcc/arm-buildroot-linux-gnueabi/8.2.0/../../../../arm-buildroot-linux-gnueabi/lib/:/home/sun/18.04-install/toolchain/arm-buildroot-linux-gnueabi/sysroot/lib/:/home/sun/18.04-install/toolchain/arm-buildroot-linux-gnueabi/sysroot/usr/lib/
38 COLLECT_GCC_OPTIONS='-std=gnu17' '-o' 'a' '-v' '-mcpu=cortex-a15' '-mfloat-abi=soft' '-mabi=aapcs-linux' '-marm' '-mtls-dialect=gnu' '-march=armv7ve'
39  /home/sun/18.04-install/toolchain/bin/../libexec/gcc/arm-buildroot-linux-gnueabi/8.2.0/collect2 -plugin /home/sun/18.04-install/toolchain/bin/../libexec/gcc/arm-buildroot-linux-gnueabi/8.2.0/liblto_plugin.so -plugin-opt=/home/sun/18.04-install/toolchain/bin/../libexec/gcc/arm-buildroot-linux-gnueabi/8.2.0/lto-wrapper -plugin-opt=-fresolution=/tmp/cc0UlL2P.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --sysroot=/home/sun/18.04-install/toolchain/arm-buildroot-linux-gnueabi/sysroot --eh-frame-hdr -dynamic-linker /lib/ld-linux.so.3 -X -m armelf_linux_eabi -o a /home/sun/18.04-install/toolchain/arm-buildroot-linux-gnueabi/sysroot/usr/lib/crt1.o /home/sun/18.04-install/toolchain/arm-buildroot-linux-gnueabi/sysroot/usr/lib/crti.o /home/sun/18.04-install/toolchain/bin/../lib/gcc/arm-buildroot-linux-gnueabi/8.2.0/crtbegin.o -L/home/sun/18.04-install/toolchain/bin/../lib/gcc/arm-buildroot-linux-gnueabi/8.2.0 -L/home/sun/18.04-install/toolchain/bin/../lib/gcc -L/home/sun/18.04-install/toolchain/bin/../lib/gcc/arm-buildroot-linux-gnueabi/8.2.0/../../../../arm-buildroot-linux-gnueabi/lib -L/home/sun/18.04-install/toolchain/arm-buildroot-linux-gnueabi/sysroot/lib -L/home/sun/18.04-install/toolchain/arm-buildroot-linux-gnueabi/sysroot/usr/lib /tmp/cch1eWdG.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /home/sun/18.04-install/toolchain/bin/../lib/gcc/arm-buildroot-linux-gnueabi/8.2.0/crtend.o /home/sun/18.04-install/toolchain/arm-buildroot-linux-gnueabi/sysroot/usr/lib/crtn.o
40 COLLECT_GCC_OPTIONS='-std=gnu17' '-o' 'a' '-v' '-mcpu=cortex-a15' '-mfloat-abi=soft' '-mabi=aapcs-linux' '-marm' '-mtls-dialect=gnu' '-march=armv7ve'
View Code

从中我们可以看到  GNU C17 字样,证明 GCC8.2 版本已经支持 C17 标准了

1 GNU C17 (Buildroot 2018.08.2) 版本 8.2.0 (arm-buildroot-linux-gnueabi)
2     由 GNU C 版本 7.3.0 编译,GMP 版本 6.1.2,MPFR 版本 3.1.6,MPC 版本 1.0.3,isl 版本 none

查看GCC 官方文档: https://gcc.gnu.org/onlinedocs/gcc/Standards.html 

 1 2 Language Standards Supported by GCC
 2 
 3 For each language compiled by GCC for which there is a standard, GCC attempts to follow one or more versions of that standard, possibly with some exceptions, and possibly with some extensions.
 4 2.1 C Language
 5 
 6 The original ANSI C standard (X3.159-1989) was ratified in 1989 and published in 1990. This standard was ratified as an ISO standard (ISO/IEC 9899:1990) later in 1990. There were no technical differences between these publications, although the sections of the ANSI standard were renumbered and became clauses in the ISO standard. The ANSI standard, but not the ISO standard, also came with a Rationale document. This standard, in both its forms, is commonly known as C89, or occasionally as C90, from the dates of ratification. To select this standard in GCC, use one of the options -ansi, -std=c90 or -std=iso9899:1990; to obtain all the diagnostics required by the standard, you should also specify -pedantic (or -pedantic-errors if you want them to be errors rather than warnings). See Options Controlling C Dialect.
 7 
 8 Errors in the 1990 ISO C standard were corrected in two Technical Corrigenda published in 1994 and 1996. GCC does not support the uncorrected version.
 9 
10 An amendment to the 1990 standard was published in 1995. This amendment added digraphs and __STDC_VERSION__ to the language, but otherwise concerned the library. This amendment is commonly known as AMD1; the amended standard is sometimes known as C94 or C95. To select this standard in GCC, use the option -std=iso9899:199409 (with, as for other standard versions, -pedantic to receive all required diagnostics).
11 
12 A new edition of the ISO C standard was published in 1999 as ISO/IEC 9899:1999, and is commonly known as C99. (While in development, drafts of this standard version were referred to as C9X.) GCC has substantially complete support for this standard version; see http://gcc.gnu.org/c99status.html for details. To select this standard, use -std=c99 or -std=iso9899:1999.
13 
14 Errors in the 1999 ISO C standard were corrected in three Technical Corrigenda published in 2001, 2004 and 2007. GCC does not support the uncorrected version.
15 
16 A fourth version of the C standard, known as C11, was published in 2011 as ISO/IEC 9899:2011. (While in development, drafts of this standard version were referred to as C1X.) GCC has substantially complete support for this standard, enabled with -std=c11 or -std=iso9899:2011. A version with corrections integrated was prepared in 2017 and published in 2018 as ISO/IEC 9899:2018; it is known as C17 and is supported with -std=c17 or -std=iso9899:2017; the corrections are also applied with -std=c11, and the only difference between the options is the value of __STDC_VERSION__.
17 
18 A further version of the C standard, known as C2X, is under development; experimental and incomplete support for this is enabled with -std=c2x.
19 
20 By default, GCC provides some extensions to the C language that, on rare occasions conflict with the C standard. See Extensions to the C Language Family. Some features that are part of the C99 standard are accepted as extensions in C90 mode, and some features that are part of the C11 standard are accepted as extensions in C90 and C99 modes. Use of the -std options listed above disables these extensions where they conflict with the C standard version selected. You may also select an extended version of the C language explicitly with -std=gnu90 (for C90 with GNU extensions), -std=gnu99 (for C99 with GNU extensions) or -std=gnu11 (for C11 with GNU extensions).
21 
22 The default, if no C language dialect options are given, is -std=gnu11.
23 
24 The ISO C standard defines (in clause 4) two classes of conforming implementation. A conforming hosted implementation supports the whole standard including all the library facilities; a conforming freestanding implementation is only required to provide certain library facilities: those in <float.h>, <limits.h>, <stdarg.h>, and <stddef.h>; since AMD1, also those in <iso646.h>; since C99, also those in <stdbool.h> and <stdint.h>; and since C11, also those in <stdalign.h> and <stdnoreturn.h>. In addition, complex types, added in C99, are not required for freestanding implementations.
25 
26 The standard also defines two environments for programs, a freestanding environment, required of all implementations and which may not have library facilities beyond those required of freestanding implementations, where the handling of program startup and termination are implementation-defined; and a hosted environment, which is not required, in which all the library facilities are provided and startup is through a function int main (void) or int main (int, char *[]). An OS kernel is an example of a program running in a freestanding environment; a program using the facilities of an operating system is an example of a program running in a hosted environment.
27 
28 GCC aims towards being usable as a conforming freestanding implementation, or as the compiler for a conforming hosted implementation. By default, it acts as the compiler for a hosted implementation, defining __STDC_HOSTED__ as 1 and presuming that when the names of ISO C functions are used, they have the semantics defined in the standard. To make it act as a conforming freestanding implementation for a freestanding environment, use the option -ffreestanding; it then defines __STDC_HOSTED__ to 0 and does not make assumptions about the meanings of function names from the standard library, with exceptions noted below. To build an OS kernel, you may well still need to make your own arrangements for linking and startup. See Options Controlling C Dialect.
29 
30 GCC does not provide the library facilities required only of hosted implementations, nor yet all the facilities required by C99 of freestanding implementations on all platforms. To use the facilities of a hosted environment, you need to find them elsewhere (for example, in the GNU C library). See Standard Libraries.
31 
32 Most of the compiler support routines used by GCC are present in libgcc, but there are a few exceptions. GCC requires the freestanding environment provide memcpy, memmove, memset and memcmp. Finally, if __builtin_trap is used, and the target does not implement the trap pattern, then GCC emits a call to abort.
33 
34 For references to Technical Corrigenda, Rationale documents and information concerning the history of C that is available online, see http://gcc.gnu.org/readings.html
35 2.2 C++ Language
36 
37 GCC supports the original ISO C++ standard published in 1998, and the 2011 and 2014 revisions.
38 
39 The original ISO C++ standard was published as the ISO standard (ISO/IEC 14882:1998) and amended by a Technical Corrigenda published in 2003 (ISO/IEC 14882:2003). These standards are referred to as C++98 and C++03, respectively. GCC implements the majority of C++98 (export is a notable exception) and most of the changes in C++03. To select this standard in GCC, use one of the options -ansi, -std=c++98, or -std=c++03; to obtain all the diagnostics required by the standard, you should also specify -pedantic (or -pedantic-errors if you want them to be errors rather than warnings).
40 
41 A revised ISO C++ standard was published in 2011 as ISO/IEC 14882:2011, and is referred to as C++11; before its publication it was commonly referred to as C++0x. C++11 contains several changes to the C++ language, all of which have been implemented in GCC. For details see https://gcc.gnu.org/projects/cxx-status.html#cxx11. To select this standard in GCC, use the option -std=c++11.
42 
43 Another revised ISO C++ standard was published in 2014 as ISO/IEC 14882:2014, and is referred to as C++14; before its publication it was sometimes referred to as C++1y. C++14 contains several further changes to the C++ language, all of which have been implemented in GCC. For details see https://gcc.gnu.org/projects/cxx-status.html#cxx14. To select this standard in GCC, use the option -std=c++14.
44 
45 The C++ language was further revised in 2017 and ISO/IEC 14882:2017 was published. This is referred to as C++17, and before publication was often referred to as C++1z. GCC supports all the changes in the new specification. For further details see https://gcc.gnu.org/projects/cxx-status.html#cxx1z. Use the option -std=c++17 to select this variant of C++.
46 
47 More information about the C++ standards is available on the ISO C++ committee’s web site at http://www.open-std.org/jtc1/sc22/wg21/.
48 
49 To obtain all the diagnostics required by any of the standard versions described above you should specify -pedantic or -pedantic-errors, otherwise GCC will allow some non-ISO C++ features as extensions. See Warning Options.
50 
51 By default, GCC also provides some additional extensions to the C++ language that on rare occasions conflict with the C++ standard. See Options Controlling C++ Dialect. Use of the -std options listed above disables these extensions where they they conflict with the C++ standard version selected. You may also select an extended version of the C++ language explicitly with -std=gnu++98 (for C++98 with GNU extensions), or -std=gnu++11 (for C++11 with GNU extensions), or -std=gnu++14 (for C++14 with GNU extensions), or -std=gnu++17 (for C++17 with GNU extensions).
52 
53 The default, if no C++ language dialect options are given, is -std=gnu++14.
54 2.3 Objective-C and Objective-C++ Languages
55 
56 GCC supports “traditional” Objective-C (also known as “Objective-C 1.0”) and contains support for the Objective-C exception and synchronization syntax. It has also support for a number of “Objective-C 2.0” language extensions, including properties, fast enumeration (only for Objective-C), method attributes and the @optional and @required keywords in protocols. GCC supports Objective-C++ and features available in Objective-C are also available in Objective-C++.
57 
58 GCC by default uses the GNU Objective-C runtime library, which is part of GCC and is not the same as the Apple/NeXT Objective-C runtime library used on Apple systems. There are a number of differences documented in this manual. The options -fgnu-runtime and -fnext-runtime allow you to switch between producing output that works with the GNU Objective-C runtime library and output that works with the Apple/NeXT Objective-C runtime library.
59 
60 There is no formal written standard for Objective-C or Objective-C++. The authoritative manual on traditional Objective-C (1.0) is “Object-Oriented Programming and the Objective-C Language”: http://www.gnustep.org/resources/documentation/ObjectivCBook.pdf is the original NeXTstep document.
61 
62 The Objective-C exception and synchronization syntax (that is, the keywords @try, @throw, @catch, @finally and @synchronized) is supported by GCC and is enabled with the option -fobjc-exceptions. The syntax is briefly documented in this manual and in the Objective-C 2.0 manuals from Apple.
63 
64 The Objective-C 2.0 language extensions and features are automatically enabled; they include properties (via the @property, @synthesize and @dynamic keywords), fast enumeration (not available in Objective-C++), attributes for methods (such as deprecated, noreturn, sentinel, format), the unused attribute for method arguments, the @package keyword for instance variables and the @optional and @required keywords in protocols. You can disable all these Objective-C 2.0 language extensions with the option -fobjc-std=objc1, which causes the compiler to recognize the same Objective-C language syntax recognized by GCC 4.0, and to produce an error if one of the new features is used.
65 
66 GCC has currently no support for non-fragile instance variables.
67 
68 The authoritative manual on Objective-C 2.0 is available from Apple:
69 
70     https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html 
71 
72 For more information concerning the history of Objective-C that is available online, see http://gcc.gnu.org/readings.html
73 2.4 Go Language
74 
75 As of the GCC 4.7.1 release, GCC supports the Go 1 language standard, described at https://golang.org/doc/go1.
76 2.5 HSA Intermediate Language (HSAIL)
77 
78 GCC can compile the binary representation (BRIG) of the HSAIL text format as described in HSA Programmer’s Reference Manual version 1.0.1. This capability is typically utilized to implement the HSA runtime API’s HSAIL finalization extension for a gcc supported processor. HSA standards are freely available at http://www.hsafoundation.com/standards/.
79 2.6 D language
80 
81 GCC supports the D 2.0 programming language. The D language itself is currently defined by its reference implementation and supporting language specification, described at https://dlang.org/spec/spec.html.
82 2.7 References for Other Languages
83 
84 See About This Guide in GNAT Reference Manual, for information on standard conformance and compatibility of the Ada compiler.
85 
86 See Standards in The GNU Fortran Compiler, for details of standards supported by GNU Fortran. 
View Code

文档中可以看到,2018年发布了C17标准

 A version with corrections integrated was prepared in 2017 and published in 2018 as ISO/IEC 9899:2018; it is known as C17 and is supported with -std=c17 or -std=iso9899:2017;

 

并且官方文档指出,默认的C标准是 GNU11 :

The default, if no C language dialect options are given, is -std=gnu11. 

指定C标准版本进行编译,验证我们的理论:

-std=gnu17

sun@machine:~$ arm-linux-gcc -std=gnu17 a.c -o a
a.c: 在函数‘audio_sem_wait’中:
a.c:140:10: 警告:implicit declaration of function ‘semtimedop’; did you mean ‘semop’? [-Wimplicit-function-declaration]
   return semtimedop(sem_id, &sem_ops, 1, NULL);
          ^~~~~~~~~~
          semop

 

-std=gnu11

sun@machine:~$ arm-linux-gcc -std=gnu11 a.c -o a
a.c: 在函数‘audio_sem_wait’中:
a.c:140:10: 警告:implicit declaration of function ‘semtimedop’; did you mean ‘semop’? [-Wimplicit-function-declaration]
   return semtimedop(sem_id, &sem_ops, 1, NULL);
          ^~~~~~~~~~
          semop

-std=gnu90 -std=gnu89 均没有任何警告

sun@machine:~$ arm-linux-gcc -std=gnu90 a.c -o a
sun@machine:~$ arm-linux-gcc -std=gnu89 a.c -o a

 

posted @ 2018-11-14 12:59  CodingTheFuture  阅读(1071)  评论(0编辑  收藏  举报