-1

I'm going to write me a little print( ) function.
However in said function I'll have to determine whether I'm dealing with a console application independent of the Operating System.
( Mostly for deciding whether to use std::cout or go with the OS related MessageBox implementation. )

If there is no auto generated constant would there be another suitable way that is multi platform compatible?

I will include it in a library, which means I can't tell which compiler will be used or which IDE.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
deW1
  • 5,562
  • 10
  • 38
  • 54
  • 1
    If you mean something like a preprocessor macro, then I doubt there is anything like that. One way I would do this, is by defining a custom macro that I would then pass to the program during compilation via the -D option. – jcxz Apr 26 '15 at 17:41
  • @jcxz : if it is for building a library, even at compile time you cannot know if it will be used by a console application or by a GUI one. In that case no compile time macro would be of any use and you will have to (try to) guess at runtime. – Serge Ballesta Apr 26 '15 at 17:46
  • 1
    @SergeBallesta Well you could still have two functions defined in your library. One console version, one gui version and in every call site you would call a macro instead of one of these two functions, that would expand to proper version based on a macro defined on compiler command line. And OP was originally asking about some sort of constant, from which I assumed that he is interested in compile time solution. – jcxz Apr 26 '15 at 18:08
  • @jcxz : You are right, the macro could be in a `.h` and as such the actual call would be determined at program compile time. Nice idea. If it was an awswer I would upvote :-) – Serge Ballesta Apr 26 '15 at 18:12
  • Yes, but this solution could not be used for prints from inside the library. For prints from inside the library one would need a runtime solution. But it is not clear from OP's question whether he wants to use this print function only internally inside his library or whether he wants users of his library to call this function. – jcxz Apr 26 '15 at 18:15

2 Answers2

1

On windows, You can combine GetConsoleWindow() together with a series of other API calls to know whether an application is of console type or GUI. Like in this post: How to check if the program is run from a console?

Also see Test if stdin has input for C++ (windows and/or linux) ,

Community
  • 1
  • 1
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
1

There is no platform independent method for determining if your application is using a GUI or a Console.

This would be exceedingly difficult on UNIX platforms, where there are many different GUI libraries, not to mention Wayland vs X11.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173