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
Asked
Active
Viewed 280 times
1

Martin Sander
- 13
- 5
-
1Judging 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
-
1Judging 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 Answers
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
-
1The way with GetConsoleWindow works! And, of course, directing output at runtime is even preferable to doing it on compile time. – Martin Sander Jun 04 '18 at 06:02
-
@MartinSander please consider accepting the answer if it answers your question. – orhtej2 Jun 04 '18 at 06:19