Glitched integrated terminal on Windows
-
Hello. I'm using Qt Creator to learn C and I'm having a strange issue with the built in terminal.
When something is printed to the terminal, for example:
printf(''hello world'');
return 0;
instead of printing:
hello world
Process exited with code: 0.
instead it prints:
h
Process exited with code: 0.ello worldSeems to me that return 0; starts printing before printf(''hello world''); has finished printing.
Currently I'm working around this problem by writing sleep(1); before return 0; and that fixes the issue.
However I would like to fix the problem properly or at least know why this happens if it can't be fixed. Thanks!
-
@Programist Works fine for me. I created a default C application in QtCreator.
How did you create the application?
Can you show the code? -
@Programist
That is because stdout is line buffered. When program exits there is a "race condition" between flushing stdout and reporting the exit code.Either terminate your
printf()
with a\n
to line-flush or put in an explicitfflush(stdout);
. -
Thanks for the responses!
I created the project by:
1: Create Project - Non-Qt Project - Plain C Application
2: Choosing location
3: Build system CMake
4: Kits Desktop Qt 6.8.0 MinGW 64-bit
Once the project is created I go to Projects/Run and tick Run in terminal.Neither '\n' or fflush(stdout); helped:
#include <stdio.h> int main() { printf("hello world\n"); fflush(stdout); return 0; }
The output with '\n' added and with or without fflush(stdout); is:
h
Process exited with code: 0.ello worldAnother example:
#include <stdio.h> int main() { printf("ABC\n"); fflush(stdout); printf("%c%c%c\n", 'D', 'E', 'F'); fflush(stdout); printf("%i%i%i\n", 1, 2, 3); fflush(stdout); return 0; }
The output of this is:
ABC
DEFcess exited with code: 0.
123If I add breakpoints at each line, run the program in debug mode and click continue manually it's fine. Also as I said in my previous post, writing sleep(1); before return 0; also fixes the problem.
ABC
DEF
123Process exited with code: 0.
-
@Programist
Then it looks like your Creator has some fundamental problem/race condition with when it outputs its "program exited" message versus the output still coming/pending from the application.Neither @jsulm nor I see this issue. It may depend on your platform, debugger and/or Creator version.
Additionally I believe Creator now has "two kinds" of terminal. In Preferences > Terminal there is a checkbox right at the top for Use internal terminal. I have that unchecked. Try yours with both the checkbox on and off, any difference?
-
@Programist I also tested on Windows (but with MSVC compiler): works fine
-
It's a bug, and it's being tracked at QTCREATORBUG-30733: Build-in Terminal does not flush output of terminating process.
-
I suggest to check "Run in terminal" under the
Project
'sRun
settings. This will open a separate terminal window for your own application.Process exited with code: 0.
should still appear in the integrated console of Qt Creator (if I'm not mistaken). -
@cristian-adam Ah ha! :)
-
@Programist said in Glitched integrated terminal on Windows:
@JonB Unchecking Use internal terminal fixes the problem
As I said, I have never used Use internal terminal. and never had a problem with the normal, external terminal. And that option was only added to Creator a while ago, it never used to have an internal terminal.
-