【问题标题】:Why does this C code works on linux but not on Windows without cygwin为什么这个 C 代码可以在 linux 上工作,但不能在没有 cygwin 的 Windows 上工作
【发布时间】:2019-04-13 19:15:25
【问题描述】:

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

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

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

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

gcc C_code.c -o binary

【问题讨论】:

    标签: c linux windows operating-system


    【解决方案1】:

    Cygwin 是一个与 POSIX 兼容的环境。当您在 Cygwin 中编译某些东西时 - 它旨在在 Cygwin 中运行。

    您需要一个 GCC 到 Windows 的端口,称为 MinGW。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 2021-01-23
      • 1970-01-01
      相关资源
      最近更新 更多