6

I'm writing some C++ with Microsoft Visual Studio 2010 Express, and I'm wondering if there is a way to display command output somewhere in the IDE instead of an external console window, or at least keep that window open.

Reading something from STDIN would work for a console application, but this is a unit test case and I don't want to modify the generated main function. Is there another way?

6 Answers 6

7

Ctrl + F5 for quick test. The key combination keeps the console open until you close it.

3
  • This. You don't need to write any artificial code to do this.
    – Tim
    May 26, 2011 at 18:12
  • Brilliant, works! Just curious: is there no menu option for this?
    – futlib
    May 30, 2011 at 10:23
  • It used to be under the Debug drop menu in MSVS 2008 Express but removed in 2010 Express. The full version of MSVS 2010 still has it under Debug: Start Without Debugging, Ctrl + F5 May 31, 2011 at 18:37
2

I've found a solution that is not really elegant, but at least it works. I'm using a fixture in my unit testing framework (Boost.Test) which does system("pause") in the tear down method:

struct Global_fixture {
    Global_fixture() {}

    ~Global_fixture()
    {
        system("pause");
    }
};
BOOST_GLOBAL_FIXTURE(Global_fixture)

I hope you guys can find a better way.

3
  • Very useful if Ctrl-F5 doesn't work (e.g. you need /SUBSYSTEM:CONSOLE for the Ctrl-F5 to work and using NMAKE/SCons I haven't figured out how to do that yet)
    – danio
    Jan 24, 2013 at 13:47
  • @danio See my answer here for how to fix the bug which prevents it working in makefile projects.
    – JBentley
    Apr 27, 2014 at 22:24
  • @JBentley thanks, SCons generates the vcxproj so patching it to be to add that tag would be a solution
    – danio
    May 7, 2014 at 10:21
1

In c++ you want to use : OutputDebugString

0

I think Debug.Write (and related) should do what you're looking for. Writes to the VS output window.

1
  • I don't think I can (or want to) fiddle with Boost.Test until it uses that instead of stdout.
    – futlib
    May 26, 2011 at 15:00
0

If you're running unit tests, you're not debugging, right? So use "Run withut debugging" and the console window will stay open.

Alternatively, open a command prompt of your own and launch the exe by typing its name.

4
  • I'm usually running in "Debug" mode, is that what you mean? Doesn't stay open.
    – futlib
    May 26, 2011 at 15:00
  • @futlib, right, I'm suggesting you run it not in Debug mode. May 26, 2011 at 15:01
  • I got the same behaviour in Release mode.
    – futlib
    May 30, 2011 at 10:23
  • Debug/Release is what kind of build you are making. I am not talking about that. Ctrl-F5 or Debug, Run Without Debugging is the command I want you to try. May 30, 2011 at 10:25
0

In VC++ use

Console::WriteLine(L"my error text");

Printf won't produce any output. Neither will OutputDebugString. The Console will write at the bottom of the test results output, so all you have to do is double-click on the test in the "Test Results" window.

1
  • There is no Console::WriteLine in VC++. You probably mixed with C#
    – nogard
    Oct 11, 2012 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.