简体   繁体   English

在Cygwin上使用TCC进行编译

[英]Compiling with TCC on Cygwin

I was testing a very basic program written in C, the entirety of which is enclosed below. 我正在测试一个用C语言编写的非常基本的程序,其全部内容如下。 Upon attempting to execute the executable produced by TCC, I noticed that I was required to enter input for each successive fgets() prior to actually seeing output from any printf() calls. 在尝试执行TCC生成的可执行文件时,我注意到在实际看到任何printf()调用的输出之前,我需要为每个连续的fgets()输入输入。

This was very confusing, so I decided to try running the executable on the standard Windows console. 这非常令人困惑,所以我决定尝试在标准Windows控制台上运行可执行文件。 It ran flawlessly, and input and output prompts were shown in the proper order. 它运行完美,输入和输出提示以正确的顺序显示。

However, I noticed that if I compiled the program with GCC it would work fine on the terminal compiled in Cygwin (mintty, though I got the same results with rxvt). 但是,我注意到如果我用GCC编译程序,它将在Cygwin编译的终端上正常工作(虽然我用rxvt得到了相同的结果,但是很好)。

Could anyone give an explanation as to why this is happening, and how I can stop it from happening? 任何人都可以解释为什么会发生这种情况,以及如何阻止它发生? I'd like to compile my programs independent of Cygwin while still using a Cygwin-based terminal. 我想编译我的程序独立于Cygwin,同时仍然使用基于Cygwin的终端。

Thanks! 谢谢!

int main()
{
        char something[12];

        printf("This printf() should be outputted before you are prompted for input: ");

        fgets(something, sizeof something, stdin);

        printf("You entered, %s", something);
}

This is down to the buffering of standard input and standard output. 这取决于标准输入和标准输出的缓冲。 I'm not sure what the C standard has to say about this (in C++ the streams are tied by default), but you can flush standard output yourself with : 我不确定C标准对此有何看法(在C ++中默认绑定流),但您可以自己刷新标准输出:

 fflush( stdout );

after the call to printf. 在打电话给printf之后。

The standard output stream is usually line-buffered, ie the buffer gets flushed when you print a newline. 标准输出流通常是行缓冲的,即在打印换行符时刷新缓冲区。

You may flush it explicitly with: 您可以使用以下方式明确刷新它

fflush(stdout);

And you can turn off buffering for a given stream s with: 您可以使用以下命令关闭给定流s缓冲:

setvbuf(s, NULL, _IONBF, 0);

See the manpage of setvbuf() for more details. 有关更多详细信息,请参见setvbuf()联机帮助页

Mintty and rxvt are terminal emulators based on Unix pseudo terminal devices . Mintty和rxvt是基于Unix 伪终端设备的终端仿真器。 Cygwin implements these based on Windows pipes . Cygwin基于Windows管道实现这些。

When you compile a program with Cygwin gcc, it gets linked to the Cygwin DLL, which contains all the magic for making streams connected to a terminal work as they should on a Unix system, which means line buffering by default. 当您使用Cygwin gcc编译程序时,它会链接到Cygwin DLL,其中包含使流连接到终端的所有工具,就像在Unix系统上一样,这意味着默认情况下会进行行缓冲。

However, when you compile the program with tcc, you create a native Windows program, and that only sees the underlying Windows pipes. 但是,当您使用tcc编译程序时,您将创建一个本机Windows程序,并且只能看到基础Windows管道。 In the Microsoft C library, streams connected to pipes are fully buffered by default, which is why flushing explicitly with fflush(stdout) or disabling the buffering with setvbuf(stdout, NULL, _IONBF, 0) helps. 在Microsoft C库中,默认情况下连接到管道的流是完全缓冲的,这就是使用fflush(stdout)显式刷新或使用setvbuf(stdout, NULL, _IONBF, 0)禁用缓冲的原因。 MS's C library does not support line buffering. MS的C库不支持行缓冲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM