Skip to main content
GameDev.net gamedev.net
🔒 Locked

cout with SDL

Started by Mr_Threepwood Jan 2, 2008 at 8:18 PM 14 replies 11.2k views
Original Post
Mr_Threepwood
Mr_Threepwood
Ok this is a stupid error that seems fine to me but for some reason isn't working. Here's my code:

#include "SDL/SDL.h"

#include <cstdlib>
#include <iostream>

int main(int argc, char *argv[])
{

    std::cout << "Program starting";

    system ("PAUSE");

    return 0;
}
 
The thing that is really vexing me is that the above code doesn't print to the console, but if I comment out the SDL header it does. I need to be able to print stuff to the console while I'm using SDL so that I can debug parts the program that I'm making (connect 4). Does anyone know why this would be happening?
Ezbez
Ezbez
Two reasons:
- With SDL the console is disabled. SDL uses windows, not consoles
- SDL redirects the standard output (both cout and printf()) to a file named... oh I can't remember now but it's like stdout.txt or something like that. It should be in the .exe's directory. You can, of course, change this using the standard library.

And finally, learn to use a good debugger. Printing values out is nice and all, but it pales in comparison to breakpoints, stepping through code, call stacks, and all the goodies that come with a debugger. Investing the time to learn how to use a debugger is truly well worth it.
Feralrath
Feralrath
if you really want to use the console to display information about your program as its running you can look at AllocConsole and its functions to create a console window and write text to it.
Megaman_22
Megaman_22
If you're using Visual Studio, you can go into the project properties and change one of the settings so that a console window does pop up. It's Linker -> System -> SubsSystem, and you make sure it's set to "Console (/SUBSYSTEM:CONSOLE)"
Eric Richards
Feralrath
Feralrath
sdl overloads the << operator so that it writes to a file not the console window so that doesn't work.
Kylotan
Kylotan
It doesn't actually overload the << operator, it just redirects the standard output to a file.
Ezbez
Ezbez
I didn't think that SDL compiled with the subsystem set to CONSOLE. Anyone care to confirm/deny that? I'm pretty sure it has to be windows.
SiCrane
SiCrane
Yes, it can.
Trenki
Trenki
The prefered way to include the SDL headers is by #include "SDL.h".

In MSVC you can simply set the subsystem in the linker settings to CONSOLE and you get a console window where cout will output.

If you are using MinGW/GCC you can simply write #undef main before the main function and SDLs init code which redirects output to a file won't be called.
This way cout also writes to the console. Doing this does not cause any problems afaik.
superpig
superpig
Quote:
Original post by Ezbez
And finally, learn to use a good debugger. Printing values out is nice and all, but it pales in comparison to breakpoints, stepping through code, call stacks, and all the goodies that come with a debugger. Investing the time to learn how to use a debugger is truly well worth it.
Seconded.
Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse
Ezbez
Ezbez
Quote:
Original post by SiCrane
Yes, it can.


Okay, but then I wonder why I was getting compiling errors just recently when I was using SDL with the CONSOLE subsytem. Perhaps it was another library I was using. Either way, good to know.
SiCrane
SiCrane
Given that /subsystem:console is a linker setting and not a compiler setting, then why you would have compiler problems is completely beyond me.
rip-off
rip-off
You'll have to post your errors. My guess is that you aren't using SDL_main.lib properly. To make SDL reasonably source compatible across different platforms with different entry points (like WinMain()) SDL provides SDL_main.lib. This library contains (on windows) an implementation of WinMain(), which calls your main() (which is actually re#defined to be SDL_main(), which is why it must take exactly 2 arguments and why you must explicitly return a value from it).

If you choose not to use SDL_main.lib's functions, you have to replicate some of the functionality it represents. Looking at the source will give you an idea of how to do this (and why its generally not done ;).

I believe there is a flag you can pass when compiling the SDL library that disables redirection (I *think* it might be no-stdio-redirect). But that is generally not a good idea (you'll have to download development versions of SDL's dependencies).

On the other hand, if you just want to resurrect console output, do this:
// outside any function#ifdef _WIN32#define WIN32_LEAN_AND_MEAN#include <windows.h>#endif// inside main or an initialisation function, after calling SDL_Init (to be safe):#ifdef _WIN32	    const char * confile = "CONOUT$";        FreeConsole();    if(  !AllocConsole() )    {        // handle error somehow.    }    else    {       freopen(confile, "w", stdout);       freopen(confile, "w", stderr);    }#endif


Works For Me TM [smile]
Ezbez
Ezbez
Quote:
Original post by SiCrane
Given that /subsystem:console is a linker setting and not a compiler setting, then why you would have compiler problems is completely beyond me.


Sorry, I meant compile-time errors by that. They really were linker errors. And now my program is compiling (err, linking) fine with a console subsystem. Oh well.
SiCrane
SiCrane
Linker errors are still not compiler time errors. They're link time errors. All compiles take place before linking begins; compiling and linking are two separate stages of putting a program together.
Ezbez
Ezbez
Okay, thanks for the correction.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.