pedantic in GCC/G++ compiler?

This note says:

-ansi : tells the compiler to implement the ANSI language option. This turns off certain "features" of GCC which are incompatible with the ANSI standard.

-pedantic : used in conjunction with -ansi , this tells the compiler to be adhere strictly to the ANSI standard, rejecting any code which is not compliant.

First things first:

  • What is the purpose of the -pedantic and -ansi options of the GCC/G++ compiler (I couldn't understand the above description)?
  • Can anyone tell me the right circumstances for using these two options?
  • When should I use them?
  • Are they important?

  • GCC compilers always try to compile your program if this is at all possible. However, in some cases, the C and C++ standards specify that certain extensions are forbidden. Conforming compilers such as gcc or g++ must issue a diagnostic when these extensions are encountered. For example, the gcc compiler's -pedantic option causes gcc to issue warnings in such cases. Using the stricter -pedantic-errors option converts such diagnostic warnings into errors that will cause compilation to fail at such points. Only those non-ISO constructs that are required to be flagged by a conforming compiler will generate warnings or errors.


    I use it all the time in my coding.

    The -ansi flag is equivalent to -std=c89 . As noted, it turns off some extensions of GCC. Adding -pedantic turns off more extensions and generates more warnings. For example, if you have a string literal longer than 509 characters, then -pedantic warns about that because it exceeds the minimum limit required by the C89 standard. That is, every C89 compiler must accept strings of length 509; they are permitted to accept longer, but if you are being pedantic, it is not portable to use longer strings, even though a compiler is permitted to accept longer strings and, without the pedantic warnings, GCC will accept them too.


    Basically, it will make your code a lot easier to compile under other compilers which also implement the ANSI standard, and, if you are careful in which libraries/api calls you use, under other operating systems/platforms.

    The first one, turns off SPECIFIC features of GCC. (-ansi) The second one, will complain about ANYTHING at all that does not adhere to the standard (not only specific features of GCC, but your constructs too.) (-pedantic).

    链接地址: http://www.djcxy.com/p/86354.html

    上一篇: g ++标准支持

    下一篇: 学习GCC / G ++编译器?