文件不会在 MS Visual Studio 中编译,但会在 GCC 中编译.为什么? [英] File won't compile in MS Visual Studio, but will in GCC. Why?

查看:25
本文介绍了文件不会在 MS Visual Studio 中编译,但会在 GCC 中编译.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这样的示例代码:

I wrote such sample code:

#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.但是我在编译过程中遇到错误(请阅读代码中的注释).

It was fine compiled and executed in Ubuntu 12.04 via gcc. I have tried to compile and run it in Windows 8 via MS Visual Studio 2012. I have created empty c++ project and created the new file: main.c. But I get errors at compiling process (read the comments in the code, please).

我不明白错误信息.我的语法不对吗?为什么会发生?

I not understand that error messages. Is my syntax not right? Why it happened?

问候

推荐答案

您正在使用 Windows 上不可用的 Unix 头文件.

You are using Unix header files which are not available on Windows.

还有一点是VC++的C编译器只支持C89.这不允许混合声明和代码.所有声明都必须位于作用域的开头.

Another thing is that VC++'s C compiler supports only C89. This does not allow mixing of declarations and code. All declarations must be at the start of a scope.

这篇关于文件不会在 MS Visual Studio 中编译,但会在 GCC 中编译.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆