[英]C program compiled with cygwin in Windows works, segmentation fault under Linux. Is cygwin GCC 'bad'?
[英]Why does this C code works on linux but not on Windows without cygwin
我有一個C代碼。
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main()
{
int a = 1;
while( a <= 5 )
{
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("Normal prinf funcation call from C\n");
fprintf(stdout, "STDOUT, Got on STDOUT from C. - now: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
fprintf(stderr, "STDERR, Got in STDERR from C. - now: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
sleep(1);
a ++;
}
return 0;
}
在Linux上,我使用gcc編譯此C代碼。 生成二進制文件。
執行二進制文件時,我看到以下內容為輸出;
Normal prinf funcation call from C
STDOUT, Got on STDOUT from C. - now: 2018-11-10 17:44:38
STDERR, Got in STDERR from C. - now: 2018-11-10 17:44:38
Normal prinf funcation call from C
STDOUT, Got on STDOUT from C. - now: 2018-11-10 17:44:39
STDERR, Got in STDERR from C. - now: 2018-11-10 17:44:39
Normal prinf funcation call from C
STDOUT, Got on STDOUT from C. - now: 2018-11-10 17:44:40
STDERR, Got in STDERR from C. - now: 2018-11-10 17:44:40
Normal prinf funcation call from C
STDOUT, Got on STDOUT from C. - now: 2018-11-10 17:44:41
STDERR, Got in STDERR from C. - now: 2018-11-10 17:44:41
Normal prinf funcation call from C
STDOUT, Got on STDOUT from C. - now: 2018-11-10 17:44:42
STDERR, Got in STDERR from C. - now: 2018-11-10 17:44:42
在Windows機器上,使用cygwin和gcc ,我將相同的C代碼編譯為.exe文件,然后嘗試在cmd中運行它(不是cygwin,可在cygwin上運行)。 屏幕上沒有任何內容。
Linux和Windows上的STDOUT / STDERR之間有什么主要區別嗎?
如何使.exe文件打印到命令提示符(至少應該執行printf調用。)?
PS:我在Linux和Windows上都使用以下命令來生成二進制文件/ exe。
gcc C_code.c -o binary
Cygwin是POSIX兼容的環境。 當您在Cygwin中編譯某些內容時-它是要在Cygwin中運行。
您需要的是GCC到Windows的端口,稱為MinGW。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.