1

I'm a Java guy (I know a fair amount of C) who is trying to edge my way into C++.

Currently I am using VisualStudio 2012 Express and have created an empty project. with the following...

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    return 0;
}

Seriously couldn't get any easier. Yet, I cannot get the damn output to show up for my life.

'Spark.exe' (Win32): Loaded 'C:\Users\Smith_000\Documents\Visual Studio 2012\Projects\Spark\Debug\Spark.exe'. Symbols loaded.
'Spark.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'Spark.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'Spark.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'Spark.exe' (Win32): Loaded 'C:\Windows\SysWOW64\apphelp.dll'. Cannot find or open the PDB file.
SHIMVIEW: ShimInfo(Complete)
'Spark.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp110d.dll'. Symbols loaded.
'Spark.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr110d.dll'. Symbols loaded.
The program '[6260] Spark.exe' has exited with code 0 (0x0).

After reading into this, it appears that creating an empty project disables the console that I've used to having in the simple C project I did in college.

So what are some deadly simple ways of getting basic debug text like printf, cout and cerr into either VS (preferred) or a console when debug mode is active?

Thanks!

Cody Smith
  • 2,732
  • 3
  • 31
  • 43
  • You can put in a breakpoint. See this: http://stackoverflow.com/questions/1775865/visual-studio-console-app-prevent-window-from-closing – chris Dec 15 '12 at 02:08
  • 2
    What kind of project did you create? You should be using the "Win32 console application" and in the options click "Empty Project". – Borgleader Dec 15 '12 at 02:26

4 Answers4

3

Route cout/cerr/clog through a custom buffer that uses OutputDebugStringA

#include <iostream>
#include <windows.h>

using namespace std;

// routes cout/cerr/clog to VC++ console and good old c stdout
class custom_outputbuffer : public std::streambuf {
public:
    custom_outputbuffer() 
    {
        setp(0, 0);

        // set std::cout to use my custom streambuf
        std::streambuf *backupOut = std::cout.rdbuf(this);
        std::streambuf *backupErr = std::cerr.rdbuf(this);
        std::streambuf *backupLog = std::clog.rdbuf(this);
    }

    ~custom_outputbuffer()
    {
        // make sure to restore the original so we don't get a crash on close!
        std::cout.rdbuf(backupOut);
        std::cerr.rdbuf(backupErr);
        std::clog.rdbuf(backupLog);
    }

    virtual int_type overflow(int_type c = traits_type::eof())
    {
        currentLine.push_back(c);
        int value = fputc(c, stdout);

        if (value == EOF || value == '\n')
        {
            OutputDebugStringA(currentLine.c_str());
            currentLine.clear();
        }

        return c;
    }

    std::string currentLine;
    std::streambuf *backupOut;
    std::streambuf *backupErr;
    std::streambuf *backupLog;
};


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

    cout << "Hello Visual Studio Debug Output" << endl;

    return 0;
}
Martin Lütke
  • 722
  • 7
  • 11
  • Very late to the party, however. I am on VS2015 and this is the first solution I came across that works for both console and UWP apps. Thanks! – gnichola Jul 12 '16 at 13:42
2

That's going to bring up a console window with your text and quickly dismiss it. You can use OutputDebugString to send info to the Output window in Visual Studio. For example,

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    OutputDebugString("Hello World\n");
    return 0;
}
Jim O'Neil
  • 23,344
  • 7
  • 42
  • 67
1

The deadly simple way:

With the cursor on the line:

return 0;

Press F9 to set a breakpoint. You should see a big red dot appear on the left hand side. That will allow you to see the console output before your program returns from main. When you're done reading the console output, press F5 to continue execution.

user974967
  • 2,928
  • 10
  • 28
  • 45
  • This isn't the problem, even with the breakpoint specified I have NO, ABSOLUTELY ZERO cout showing up in the Output window. – Cody Smith Dec 15 '12 at 02:27
0

To see text in the Windows IDE output you use OutputDebugString().

See http://msdn.microsoft.com/en-us/library/windows/desktop/aa363362%28v=vs.85%29.aspx

Otherwise it goes to the console (see your settings if you want a console, but know that it will close as soon as your program exits, to prevent such use something like getchar() to pause until you hit enter.)

Better, to my point of view, switch to Linux...

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156