Rectangle 27 0

c++ What is the purpose of using pedantic in GCCG++ compiler?


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

Note
Rectangle 27 0

c++ What is the purpose of using pedantic in GCCG++ compiler?


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

The ISO C and C++ standards only "prohibit extensions" in that the extension must not change the behaviour of any conforming program. The compiler is not forced to reject any program that uses extensions.

Note
Rectangle 27 0

c++ What is the purpose of using pedantic in GCCG++ compiler?


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

@slf: Because like every other vendor (albeit that GNU doesn't sell their compiler for cash), they would like you to use their proprietary features? Or, more generally, because they consider the extensions useful and think they should be enabled by default.

For whatever it is worth, and JFTR, I've mostly stopped using -pedantic, but most of my code still compiles OK when I re-enable it (the one program that didn't was explicitly using __int128 types, which are pedantically incorrect). I think there was an intervening stage when GCC was too noisy (for my liking) with -pedantic. I just tested about 300 source files some library code, some commands, some SO test programs and there was just the one to-be-expected problem. Currently using GCC 4.8.2 on Mac OS X 10.9.2.

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.

Why is this not defaulted to true? Why have to opt-in for it rather than opt-out?

Note
Rectangle 27 0

c++ What is the purpose of using pedantic in GCCG++ compiler?


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

@slf: Because like every other vendor (albeit that GNU doesn't sell their compiler for cash), they would like you to use their proprietary features? Or, more generally, because they consider the extensions useful and think they should be enabled by default.

For whatever it is worth, and JFTR, I've mostly stopped using -pedantic, but most of my code still compiles OK when I re-enable it (the one program that didn't was explicitly using __int128 types, which are pedantically incorrect). I think there was an intervening stage when GCC was too noisy (for my liking) with -pedantic. I just tested about 300 source files some library code, some commands, some SO test programs and there was just the one to-be-expected problem. Currently using GCC 4.8.2 on Mac OS X 10.9.2.

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.

Why is this not defaulted to true? Why have to opt-in for it rather than opt-out?

Note
Rectangle 27 0

c++ What is the purpose of using pedantic in GCCG++ compiler?


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

The ISO C and C++ standards only "prohibit extensions" in that the extension must not change the behaviour of any conforming program. The compiler is not forced to reject any program that uses extensions.

Note
Rectangle 27 0

c++ What is the purpose of using pedantic in GCCG++ compiler?


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

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.

One example is gcc's "__asm__()" inline assembly code that is fine for gcc but that double underscore thing may not work on say a Windows compiler, even if that compiler did comply with the standard.

You mention double-underscore stuff, this sound interesting. What exactly are you refering to?

Note
Rectangle 27 0

c++ What is the purpose of using pedantic in GCCG++ compiler?


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

Note