2012-09-30 34 views
0

我寫了這樣的示例代碼:文件不會在MS Visual Studio中編譯,但會在GCC中編譯。爲什麼?

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

這是罰款編譯並通過GCC在Ubuntu 12.04中執行。我試圖通過MS Visual Studio 2012在Windows 8中編譯和運行它。我創建了空的C++項目並創建了新文件:main.c.但是我在編譯過程中遇到錯誤(請閱讀代碼中的註釋)。

我不明白,錯誤消息。我的語法不正確?爲什麼發生?

問候

+0

這編譯罰款ideone([鏈接](http://ideone.com/1vZrU))。當然它不會運行:) – dasblinkenlight

+0

「沒有這樣的文件或目錄」 我的猜測是,你有一個鏈接問題。對於MS Visual Studio,一些庫可能必須手動設置才能編譯。 – Araw

+0

閱讀編譯器警告和錯誤的第一條規則:只有第一條是重要的,所有後續的消息可能會受到第一條的影響,可能實際上並不是一個錯誤。 – stefan

回答

3

使用的是UNIX的頭文件不屬於Windows上可用。另一件事是VC++的C編譯器只支持C89。這不允許混合聲明和代碼。所有聲明都必須在範圍的開始處。

+0

但它現在工作。 –

相關問題