File won't compile in MS Visual Studio, but will in GCC. Why?

对着背影说爱祢 提交于 2021-01-29 17:44:43

问题


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];
}

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?

Regards


回答1:


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

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.



来源:https://stackoverflow.com/questions/12662326/file-wont-compile-in-ms-visual-studio-but-will-in-gcc-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!