我有一个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机器上,我使用cygwin和gcc将相同的C代码编译成.exe文件,然后尝试在cmd中运行它(不是cygwin,工作在cygwin上)。屏幕上什么都不印。
在Linux和Windows上,STDOUT/STDERR之间有什么重大区别吗?
如何将.exe文件打印到命令提示符(至少printf调用应该有效)?
P.S:我在Linux和Windows上都使用以下命令来生成二进制/exe。
gcc C_code.c -o binary发布于 2018-11-10 12:31:53
Cygwin是一个与POSIX兼容的环境。当您在Cygwin中编译某些内容时,它意味着在Cygwin中运行。
您需要的是GCC到Windows的端口,称为MinGW。
https://stackoverflow.com/questions/53238925
复制相似问题