好吧,我已经完成了使用 Pelles C 编写的 C 语言编程作业,到目前为止一切顺利,但最终它需要在大学的 Unix 系统上运行。所以我尝试在他们的 Unix 上编译,突然我遇到了一堆以前不存在的错误。
假设我将其作为主要功能:
#include <stdio.h>
int main (int argc, char* argv[]){
printf("Hello World");
int anInt;
return 0;
}
然后我给出一个编译命令...
cc main.c
我收到这个错误:
“main.c”,第 5 行:int 之前或处的语法错误
...这是否是 Internet 上到处都是 Unix 命令的常见示例但实际上并不是您曾经使用过的那种情况?还是 Pelles C 在这里为我“填补空白”?还是那个编译器有问题,或者什么?
最佳答案
我以前没见过这个。当我在我的 Ubuntu 服务器上尝试时,您的源代码编译正常。但是,当我尝试使用 -pedantic
标志时,我收到以下错误:
hello.c: In function ‘main’:
hello.c:5:5: warning: ISO C90 forbids mixed declarations and code [-Wpedantic]
int anInt;
^
因此,您的解决方案是找到一个支持晚于 C90 的标准的编译器,或者更改您的源代码以将声明移到代码之前:
#include <stdio.h>
int main (int argc, char* argv[]){
int anInt;
printf("Hello World");
return 0;
}
更准确地说,变量必须在其作用域所在的 block 中的代码之前声明,因此这也是有效的:
#include <stdio.h>
int main (int argc, char* argv[]){
printf("Hello ");
{
int anInt;
printf(" World");
}
return 0;
}
关于c - 仅 Unix 编译器的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21359647/