![](/img/trans.png)
[英]C code works on Windows (compiled with Visual Studio) but won't compile on Linux (using gcc)
[英]File won't compile in MS Visual Studio, but will in GCC. Why?
我寫了這樣的示例代碼:
#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。 但是我在編譯過程中出錯(請閱讀代碼中的注釋)。
我不明白該錯誤消息。 我的語法不對嗎? 為什么會這樣?
問候
您正在使用Windows上不可用的Unix頭文件。
另一件事是VC ++的C編譯器僅支持C89。 這不允許混合使用聲明和代碼。 所有聲明必須在作用域的開頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.