Nagri
Nagri

Reputation: 3136

Why does this C code works on linux but not on Windows without cygwin

I have a C code.

#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 I compile this C code with gcc. A binary gets generated.

I see the following as output, when I execute the binary;

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

On windows machine, using cygwin and gcc, I compile the same C code to a .exe file, Then try to run it in the cmd (not cygwin, works on cygwin). Nothing gets printed on the screen.

Is there any major difference between STDOUT/STDERR on Linux and on Windows?

How Can I make the .exe file print to command prompt(At least printf call should have worked.)?

P.S: I use the following command on both Linux and Windows to generate the binary/exe.

gcc C_code.c -o binary

Upvotes: 0

Views: 277

Answers (1)

Zaur Nasibov
Zaur Nasibov

Reputation: 22659

Cygwin is a POSIX-compatible environment. When you compile something in Cygwin - it is meant to run in Cygwin.

What you need is a port of GCC to Windows, called MinGW.

Upvotes: 2

Related Questions