1

When I build a simple console app with clang, it works fine:

void main() { puts("HELLO"); }

But when I create a Windows app with WinMain, I can't see stdout.

There must be a flag that fixes it, like MinGW's -mconsole

Alex
  • 34,581
  • 26
  • 91
  • 135
  • You can open a console window with: [`AllocConsole()`](https://learn.microsoft.com/en-us/windows/console/allocconsole). – 001 Apr 01 '19 at 15:12
  • Clang is just the compiler, it is the linker that determines what subsystem you target. So you'll have to tell us what linker you use. – Hans Passant Apr 01 '19 at 16:09
  • @HansPassant clang uses link.exe – Alex Apr 01 '19 at 17:36
  • I never had trouble using ordinary `main` function from within GUI applications, too, so far. I wouldn't use WinMain, no matter whatever else MS guys tell... – Aconcagua Apr 02 '19 at 11:43

2 Answers2

4

A quick stdout-enabler for otherwise GUI apps:

if (AllocConsole())
{
    FILE* fi = 0;
    freopen_s(&fi, "CONOUT$", "w", stdout);
}

and then std::cout and printf work.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
2

WinMain is a custom microsoft entry function for a windows graphical application (with windows and menus etc). It doesn't have a console by default.

If you want a console program you should just use the standard main function.

If you want a graphical application (WinMain) that also has a console, then that's a little bit of work. Check How do I get console output in C++ with a Windows program? on how to achieve that.

bolov
  • 72,283
  • 15
  • 145
  • 224