I'm using GCC 4.3.3 on Ubuntu 9.04 64-bit and was getting errors using C++-style comments in C code. When I say "by default" in the title, I mean simply invoking gcc te开发者_StackOverflow社区st.c
According to the GCC 4.3.3 docs (here), this is supported...yet I got the errors anyway.
These errors went away with a simple -std=c99 addition to my compile string, so my problem is solved. Curious if any GCC experts out there had an explanation for this, as it seems to me to be a clear contradiction to the documentation.
#include <stdio.h>
// this is a comment
int main( void )
{
return 0;
}
It's possible Ubuntu is overriding the default, which should be gnu89. Certainly I don't get that with my copy of GCC 4.3 (on Debian).
% echo '// foo' | gcc-4.3 -x c -c -
% echo '// foo' | gcc-4.3 -std=gnu89 -x c -c -
% echo '// foo' | gcc-4.3 -std=c89 -x c -c -
<stdin>:1: error: expected identifier or '(' before '/' token
Quote from http://gcc.gnu.org/onlinedocs/gcc/Standards.html#Standards
The default, if no C language dialect options are given, is -std=gnu89
And // comments
are recognized by -std=gnu89
By defualt GCC is using C89/90 standard with GCC extensions. Strictly speaking by default it is not adhering to any specific standard, since by default it will not issue any diagnostic messages in situations when such messages are required by the standard. You need to run gcc in -ansi -pedantic
mode (possibly also -Wall
) in order to make it stick to the standard. And in this case you'll have, once again, C89/90.
精彩评论