Reputation: 8220
I am programming a game in C++. I want to print debugging messages from within my code using std::cout, but as this is a GUI application there is no console to print to by default. I have tried simply running it from CMD like this:
start Debug/hydro.exe
But to no avail.
On Windows 8 x86_64 using Visual Studio 2012 with a Win32 project, the following code achieves what I am looking for:
#include <Windows.h>
...
AllocConsole();
freopen("CONIN$", "r",stdin);
freopen("CONOUT$", "w",stdout);
freopen("CONOUT$", "w",stderr);
However, I believe that this not cross-platform (do correct me if I am wrong!) and would like my application to work on Linux and Mac OS X.
Is there a cross-platform solution to this? Of course, the simpler the better!
Upvotes: 0
Views: 668
Reputation: 129324
I'm not aware of any product that does this in a Linux world - you are expected to start the code from a shell, and the output appears in the shell window.
Wrapping like this should help:
#ifdef _WIN32
// Windows-only code here
#endif
When outside of Windows, the code will not be compiled, when inside of the Windows the code will be compiled!
Upvotes: 1
Reputation: 8677
If your code depends on <Windows.h>
it is DEFINITELY NOT cross-platform.
Take a look at log4cxx for cross-platform logging. It's a pretty stable, mature platform.
PS: Good luck with cross-platform GUI programming. GUI programming is typically very platform-specific. A framework like Qt will probably help a lot, and if you are doing 3D graphics, be sure to use OpenGL rather than DirectX.
Upvotes: 0