19

I am trying to use "printf" in my Visual C++ project however it is not working. Using Lazy Foo's tutorial, I set up SDL in my project, but when I play it, printf doesnt do anything.

#include "SDL.h"
#include <stdio.h>

int main( int argc, char* args[] ) {
    printf("Testing");
    return 0;
}

The output looks like this:

The program '[4664] SDL Testing.exe: Native' has exited with code 0 (0x0).

And that's about it. What could be wrong?

Qasim
  • 1,686
  • 4
  • 27
  • 51
  • Usually `0` signifies success. Try to take any input (or) keep a break point at return statement. – Mahesh Jun 17 '12 at 02:50
  • @Mahesh I tried putting breakpoints during the time I used printf, still no output. :( – Qasim Jun 17 '12 at 02:52
  • The output window in Visual Studio != stdout or stderr. – ta.speot.is Jun 17 '12 at 03:04
  • @ta.speot.is Is there any way to see the output in Visual Studio? – Qasim Jun 17 '12 at 03:06
  • Try putting the 'ugly' getch() before the `return` statement to see the console output. A less 'ugly' method is to use `char c; std::cin>>c;` before the `return` statement. The other (better) way to see the output is by running the executable from the command line. – A. K. Jun 17 '12 at 03:07
  • @AdityaKumar CTRL + F5 didnt work and neither did adding "getchar();" before the return statement. How would I compile from the command line? – Qasim Jun 17 '12 at 03:13
  • Are you using SDL properly? See http://www.cplusplus.com/forum/general/11692/ for `SDL_main` vs. `main` shenanigans. Either that or your application is not a console application, I feel. – ta.speot.is Jun 17 '12 at 03:13
  • @Qasim http://msdn.microsoft.com/en-us/library/ms235639(v=vs.100).aspx – A. K. Jun 17 '12 at 03:15
  • Yes, I am using guide here: http://www.lazyfoo.net/SDL_tutorials And followed everything without mistake. I have even created 3 new projects to see if I might have gotten something wrong – Qasim Jun 17 '12 at 03:15
  • @AdityaKumar I get errors on the main.cpp not being able to find SDL libraries. I don't think command line will solve the problem. I would really think an IDE as professional as Visual C++ would show printf? What could I be doing wrong? – Qasim Jun 17 '12 at 03:19
  • Is the console window closing before you can see any output or does it show nothing? – Shawn Buckley Jun 17 '12 at 03:24
  • @ShawnB When I use F5, the window labeled "Output" shows for a second but then goes away. I open it, and it contains the following contents: pastebin.com/UKWpCKsD When I use CTRL+F5 it stays, but has the same contents. – Qasim Jun 17 '12 at 03:25
  • If you switched `printf` with `std::cout` does it change? – Shawn Buckley Jun 17 '12 at 03:27
  • Also the threads at bottom exiting are good, its likely SDL (SDL spawns a couple of threads when it runs). – Shawn Buckley Jun 17 '12 at 03:28
  • @ShawnB There is still no output. This is really discouraging as I am trying to learn c++. There must be an error in how I set things up but I have re-done everything now up to 5 times, following a guide that is praised by many. – Qasim Jun 17 '12 at 03:32
  • The guide is excellent, I have used it extensively to learn SDL myself. I hate to say this, but you should probably ignore this issue and continue on. If you have your build environment set up, try to run a single SDL command like `SDL_Init()` to make sure it links correctly. If it does, just follow the guide as normal. – Shawn Buckley Jun 17 '12 at 03:43
  • @ShawnB Everything works, I have even displayed an image to the screen, but I cannot program without having someway to output messages. I just can't, it is fundamental to the way I develop games. – Qasim Jun 17 '12 at 03:46
  • I find console printing to be flakey in Windows; I use files instead for that purpose. You are absolutely right though, there is no other way of getting raw information about what you are doing. – Shawn Buckley Jun 17 '12 at 03:56
  • In my case, I was running the exe with " | tee-object -filePath tee.txt" option. It prevented printf somehow. – nurp Feb 12 '20 at 08:45

8 Answers8

19

Bring up the projects properties, go to linker->system->subsystem and change it to the third option, CONSOLE. That should do it

DaKrazyKatt
  • 199
  • 1
  • 3
10

SDL by default redirects stdout to a file, stdout.txt. You should find it in your program's working directory.

jrok
  • 54,456
  • 9
  • 109
  • 141
  • Thank you. This file is created in Visual C++ project Debug folder, containing all the "std::cout" functions I call. – Qasim Jun 17 '12 at 14:19
8

In Linker -> System in your project's properties, check that the SubSystem is "Console (/SUBSYSTEM:CONSOLE)".

That causes a separate console window to be brought up when you run your program. If your current entry point isn't main, then you'll need to change it to that if you do this though.

thiagoh
  • 7,098
  • 8
  • 51
  • 77
  • And to change the entry point in VC++2005, add `/ENTRY:"WinMainCRTStartup"` to Configuration Properties > Linker > Command Line > Additional options. – andreszs Apr 21 '23 at 14:16
6

Everything works, I have even displayed an image to the screen, but I cannot program without having someway to output messages

I assume this means that your have a window available to you, not a console.

If you want to log something to the output window, use OutputDebugString:

Sends a string to the debugger for display.

void WINAPI OutputDebugString(
  __in_opt  LPCTSTR lpOutputString
);

Header WinBase.h (include Windows.h)

ta.speot.is
  • 26,914
  • 8
  • 68
  • 96
4

Printf usually needs a newline to update the console. Add a '\n' character to the end and re-run the program.

Shawn Buckley
  • 506
  • 6
  • 16
  • Sadly there is still no output! I am wondering what could be causing this. Very absurd. – Qasim Jun 17 '12 at 03:03
3

Try defining NO_STDIO_REDIRECT.

#define NO_STDIO_REDIRECT

If that doesn't work try the solution in this link: How can I get console output instead of stdout.txt and stderr.txt?.

talles
  • 14,356
  • 8
  • 45
  • 58
  • 1
    This is for SDL1.2 only, does not work for SDL2. As you search the source code for SDL2, you will see this symbol is not being used. – Bram Jan 31 '15 at 21:12
0

You're probably not seeing the output because you're running the program from within Visual Studio by pressing F5 and the console window closes after the program exits.

You can do one of the 3 things -
Put a breakpoint at the return statement.
Put a getchar() statement before the return statement.
Run the program by pressing Ctrl+F5 instead of F5.

All of the above will cause the console window to remain on the screen.

You could also directly run the EXE from a command prompt (cmd.exe).

Superman
  • 3,027
  • 1
  • 15
  • 10
  • 1
    Putting a breakpoint at the return statement, I don't know where to look for the console. When pressing Ctrl+F5, the Output view remains empty, but when I just use F5 the output shows build logs and debug logs, with it containing the following: http://pastebin.com/UKWpCKsD Do all the PDB errors play a role in this? – Qasim Jun 17 '12 at 03:01
  • The PDB errors that you're seeing don't matter. Check the return value of printf. It returns the number of characters that has been printed. printf outputs characters in a separate console window and not in the output view. – Superman Jun 17 '12 at 03:41
  • how do I get access to the seperate console window? – Qasim Jun 17 '12 at 03:43
  • When you do Ctrl+F5 there will a console window open. Check your taskbar. – Superman Jun 17 '12 at 03:45
  • No console is ever opened. It is just the output window. – Qasim Jun 17 '12 at 03:47
  • Looks like you've not created a console application. printf outputs to the console. Use OutputDebugString instead. – Superman Jun 17 '12 at 03:49
-4

there is an output window of visual studio when you are running/debugging your program. You should be able to see the output in that window.

Tariq Mehmood
  • 259
  • 1
  • 5
  • 15