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

在 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 调用应该有效。)?

PS:我在 Linux 和 Windows 上都使用以下命令来生成二进制文件/exe。

gcc C_code.c -o binary
4

1 回答 1

2

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

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

于 2018-11-10T12:31:53.897 回答