Sign in to follow this  
Mr_Threepwood

cout with SDL

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)"

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Given that /subsystem:console is a linker setting and not a compiler setting, then why you would have compiler problems is completely beyond me.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this