tags:

views:

175

answers:

5

Hi,

-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.

Text above is extract from this note. First thing first, what is the purpose of usage of -pedantic and -ansi option with GCC/G++ compiler, I couldn't understand the above description. Can anyone tell me the circumstances of using the above two options? When should I use it? Is it important one?

THanks @!

+1  A: 

If your code needs to be portable then you can test that it compiles without any gcc extensions or other non-standard features. If your code compiles with -pedantic -ansi then in theory it should compile OK with any other ANSI standard compiler.

Paul R
`-pedantic` doesn't turn off all extensions, it leaves on a bunch of double-underscore stuff. So it might be more accurate to say that if your code compiles with `-pedantic -ansi`, and it also looks plausibly like it might compile on other implementations, then it will compile.
Steve Jessop
You mention double-underscore stuff, this sound interesting. What exactly are you refering to?
huahsin68
A: 

If you're writing code that you envisage is going to be compiled on a wide variety of platforms, with a number of different compilers, then using these flags yourself will help to ensure you don't produce code that only compiles under GCC.

Damien_The_Unbeliever
+7  A: 

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.

Jonathan Leffler
-ansi turns off some extensions of GCC whereas -pedantic turns off more extensions. It seems like -ansi is first level rule then -pedantic is more restricted rule. Does it mean with this two options remove I can have my code more compatible to other compiler like Microsoft one?
huahsin68
@huahsin68: well, more or less. MSVC is a rather different compiler from most others, more adapted to its particular ecosystem and not available outside it. It does a lot of things its own way - which is not the same as the standard. Nevertheless, if you stay with the standard headers and so on, then MSVC and GCC are fairly similar. However, using `-std=c89 -pedantic` does mean you can move more easily between different compilers on other platforms. As soon as you start using `<windows.h>`, compatibility with other systems becomes problematic.
Jonathan Leffler
+2  A: 

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).

Francisco Soto
A: 

Pedantic makes it so that the gcc compiler rejects all GNU C extensions not just the ones that make it ANSI compatible.

teehoo
This is so interesing. You mention GNU C extension, and those extension may and may not be in ANSI standard. Can I have more info regarding to this. Where can I get those appropriate resource?
huahsin68