c - 文件不会在 MS Visual Studio 中编译,但会在 GCC 中编译。为什么?

标签 c visual-studio gcc c89

我写了这样的示例代码:

#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/

相关文章:

c - 当我向它添加 0.1 时, float 不会改变

c# - 控制台跟踪监听器 : Don't write to VS output window

c# - Surface 2.0 SDK 还在使用吗?

javascript - 显示密码计强度级别

python - 无法在 OS X Lion 中构建 Cython/distutils。

c - 如何通过单个 Makefile 使用不同的标志组合自动编译代码

c - 这是用两个线程初始化信号量的正确方法吗

c - 以字符串形式读取文件

将 void 指针转换为结构并对其进行初始化

C:#include 中的前缀