首页 文章

为什么这个C代码适用于Linux而不适用于没有cygwin的Windows

提问于
浏览
0

我有一个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;
}

On 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机器上,使用 cygwingcc ,我将相同的C代码编译为 .exe 文件,然后尝试在cmd中运行它(不是cygwin,适用于cygwin) . 屏幕上没有任何内容 .

Linux和Windows上的STDOUT / STDERR有什么主要区别吗?

如何将 .exe 文件打印到命令提示符(至少printf调用应该有效 . )?

P.S:我在Linux和Windows上使用以下命令来生成二进制文件/ exe .

gcc C_code.c -o binary

1 回答

  • 2

    Cygwin是POSIX兼容的环境 . 当您在Cygwin中编译某些东西时 - 它意味着在Cygwin中运行 .

    你需要的是一个GCC到Windows的端口,叫做MinGW .

相关问题