C语言 如何在C99模式下编译C项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15870567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compile a C project in C99 mode?
提问by mpluse
I got the following error message while compiling the C code:
我在编译 C 代码时收到以下错误消息:
error: 'for' loop initial declarations are only allowed in C99 mode
note: use option -std=c99 or -std=gnu99 to compile your code
What does it mean?
这是什么意思?
How to fix it?
如何解决?
回答by Blorgbeard is out
You have done this:
你已经这样做了:
for (int i=0;i<10;i++) {
And you need to change it to this:
你需要把它改成这样:
int i;
for (i=0;i<10;i++) {
Or, as the error says,
或者,正如错误所说,
use option -std=c99 or -std=gnu99 to compile your code.
使用选项 -std=c99 或 -std=gnu99 编译您的代码。
Updatecopied from Ryan Fox's answer:
从 Ryan Fox 的回答中复制的更新:
gcc -std=c99 foo.c -o foo
Or, if you're using a standard makefile, add it to the CFLAGS variable.
或者,如果您使用的是标准 makefile,请将其添加到 CFLAGS 变量中。
回答by luser droog
You'll still need C99 if you want to mix statements and variable declarations. As other answers and the error message itself say, add -std=c99to the command-line when you compile to enable C99 features [1].
如果您想混合使用语句和变量声明,您仍然需要 C99。正如其他答案和错误消息本身所说,-std=c99在编译时添加到命令行以启用 C99 功能 [1]。
But you have always been allowed to write a compound statement (a "block", IOW, but the standard never uses this word!) in place of a single statement.
但是你总是被允许写一个复合语句(一个“块”,IOW,但标准从不使用这个词!)代替单个语句。
#include<stdio.h>
int main() {
int i = 5;
{ /* new block, new declarations. */
int i;
for (i=0;i<10;i++){
}
}
printf("%d\n", i); /* prints "5\n" */
}
This is legal in K&R, C90 (aka C89, it's the same thing), andC99.
这在 K&R、C90(又名 C89,是同一回事)和C99 中是合法的。
Enabling C99 mode gets you lots of cool stuff, but it also disables some other cool stuff that gcc allows by default, like anonymous structures and unions within structures and unions.
启用 C99 模式会给你带来很多很酷的东西,但它也会禁用 gcc 默认允许的一些其他很酷的东西,比如结构和联合中的匿名结构和联合。
-std=gnu99probably enables "all the goodies", but I caution you to avoid doing this. It will make unnecessary difficulty if you (or others) wish to port the code. I'd probably have a windows version of my pet project, ported for free by somebody, had I not done this very thing. It ties you gcc. You don't want to be tied. That's the whole bloody point of standards.
-std=gnu99可能会启用“所有好东西”,但我提醒您避免这样做。如果您(或其他人)希望移植代码,这将带来不必要的困难。如果我没有做过这件事,我可能会有一个 Windows 版本的我的宠物项目,由某人免费移植。它与你 gcc 联系在一起。你不想被束缚。这就是标准的全部血腥点。
回答by Ryan Fox
The other answers give you a work around to deal with GCC's default mode. If you'd like to use C99, (which I do recommend in general) then you have to add that compiler flag:
其他答案为您提供了处理 GCC 默认模式的解决方法。如果您想使用 C99,(我通常建议这样做),那么您必须添加该编译器标志:
gcc -std=c99 foo.c -o foo
Or, if you're using a standard makefile, add it to the CFLAGS variable.
或者,如果您使用的是标准 makefile,请将其添加到 CFLAGS 变量中。
回答by Ryan Fox
It means you can't declare variables in forstatement.
这意味着您不能在for语句中声明变量。
You should do:
你应该做:
int i ;
for( i = 0 ; i < len ; i++ )
What you are probably doing
你可能在做什么
for( int i = 0 ; i < len ; i++ )

