我写了这样的示例代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
char* print_errno_msg(int value);
int main(void){
struct stat buffer;
int status;
status = stat("./main.c", &buffer);
char *msg = print_errno_msg(errno); /* syntax error : missing ';' before 'type' */
printf("status = %i; errno = %i; %s\n", status, errno, msg); /* 'msg' : undeclared identifier */
errno = 0;
int ch = getchar(); /* syntax error : missing ';' before 'type' */
return 0;
}
char* print_errno_msg(int value){
static char *messages[] = {
"",
"Operation not permitted",
"No such file or directory",
"No such process"
};
return messages[value];
}
它在 Ubuntu 12.04 中通过 gcc 编译和执行得很好。我试图通过 MS Visual Studio 2012 在 Windows 8 中编译和运行它。我创建了空的 c++ 项目并创建了新文件:main.c。但是我在编译过程中遇到错误(请阅读代码中的注释)。
我不明白错误信息。我的语法不对吗?为什么会这样?
问候
最佳答案
您正在使用 Windows 上不可用的 Unix 头文件。
另一件事是VC++的C编译器只支持C89。这不允许混合声明和代码。所有声明都必须位于范围的开头。
关于c - 文件不会在 MS Visual Studio 中编译,但会在 GCC 中编译。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12662326/