【发布时间】:2013-08-22 09:24:33
【问题描述】:
我制作了一个简单的 C 程序,并尝试使用 gcc 编译器对其进行编译。但是当我执行gcc test.c -o test 时会抛出一些错误:
test.c:1:19: error: cstdlib: No such file or directory
test.c:2:20: error: iostream: No such file or directory
test.c: In function ‘main’:
test.c:8: error: ‘for’ loop initial declaration used outside C99 mode
而且我的 C 程序非常简单,所以我认为问题不在于代码:
#include <cstdlib>
#include <iostream>
int main(int args, char **argv){
int result[500];
for (int i = 0; i < sizeof(result); ++i){
result[i] = 1;
}
return 0;
}
谢谢!
【问题讨论】:
-
你需要使用c++头文件使用g++。对于您的此代码,您不需要任何头文件。
-
你要么在 C++ 中编码(然后需要使用
g++而不是gcc),要么在 C 中(然后摆脱 C++ 包含)。 -
为了澄清上述内容,将包含更改为“stdlib.h”和“stdio.h”
-
另一件事:sizeof 给出了字符的大小,而不是数组中的位置数,所以你需要写 i .
-
如果使用 gcc 编译,请添加 -std=c99。在 for 循环头中定义变量是 C99 中的新功能。
标签: c gcc for-loop compiler-construction main