Recently, I tried to compile a simple console hello world application for the Windows operating system with Mingw from Linux:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv) {
for(int i = 0; i < 10; i++) {
printf("i = %d\n", i);
sleep(1);
}
}
$ i686-w64-mingw32-gcc hello.c -o hello.exe -mconsole
To my surprise, when I run it from Windows Console (cmd.exe) it creates a separate console window rather than printing my stdout messages to the same console window. What's even worse, I couldn't find my problem's description anywhere, even here on SO. For comparison, I tried running a 3rd-party console program from Windows Console (usbip.exe), but just as I expected, it worked like a charm.
Is such behavior a kind of bug in my Mingw-W64 C standard library version or somewhat expected? Is there any way to get rid from this annoying behavior?
upd. Some people suggested omitting -mconsole during the compilation process. Unfortunately, it works in exactly the same way. I've even tried -mwindows and this way it doesn't opens a new console window, but doesn't prints anything to my console window either (and actually returns the control to cmd.exe immediately just as expected when running executables belonging to -mwindows subsystem).
After messing up for a while, it turned out to be a Windows Defender-related issue. I compiled another console hello world program with another filename not known to Windows yet, and this time it worked as expected.
upd2. Finally, this turns out to be related to something known as the "Installer Detection Technology of UAC". The way to avoid this behavior is to specify a corresponding
requestedExecutionLevelin the manifest: https://superuser.com/questions/1677002/mechanism-of-windows-uac-shield-buckler-appendage-on-exe-files