1

Probably this is an easy or stupid question, but I cannot find the answer: Is it possible to check at compile time, if GCC on Windows is called with the -mwindows or the -mconsole option? I would like to direct output to stdout or to a message box, depending on this option, like

#ifdef CONSOLE_BUILD
printf( "Hello on stdout" );
#elifdef GUI_BUILD
MessageBoxW( NULL, L"Hello on GUI", NULL, MB_OK );
#endif

  • 1
    Judging from [this post](https://gcc.gnu.org/ml/gcc-help/2004-01/msg00225.html) there's difference in `__subsystem__` symbol, worth checking it out. – orhtej2 Jun 03 '18 at 19:06
  • 1
    Judging from [this answer](https://stackoverflow.com/a/13872211/7034621) `-mconsole` results in `__subsystem__` being `3` and `-mwindows` - `2`. – orhtej2 Jun 03 '18 at 19:09

1 Answers1

1

-mconsole and -mwindows only affect the linker, not the compiler. You could use a GCC specs file to add -DCONSOLE_BUILD whenever -mconsole is specified, but that would essentially be a custom GCC modification.

As an alternative, there seem to be various ways to figure out if a console window is associated with a process, for example the GetConsoleWindow function (but I'm not a Windows programmer).

Florian Weimer
  • 32,022
  • 3
  • 48
  • 92