9

I started a blank project in Visual Studio 2010 to write a C application. How can I send debug information to the Output window (menu Debug -> Windows -> Output )? Is there a relatively simple way to implement TRACE or OutputDebugString or something similar?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Jim Fell
  • 13,052
  • 32
  • 122
  • 196
  • possible duplicate of [Printing output on the Output Window in Visual C++ IDE](http://stackoverflow.com/questions/7697842/printing-output-on-the-output-window-in-visual-c-ide) – Autodidact Mar 09 '15 at 12:13
  • Also see: http://stackoverflow.com/q/1333527/39648 – Autodidact Mar 09 '15 at 12:25

3 Answers3

8

You can use OutputDebugString from a VS C program.

#include <windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
    OutputDebugString(_T("Hello World\n"));
    return 0;
}

The output will only be visible if you run with debugging (Debug > Start Debugging)

In the Output window, select "Debug" for "Show output from:"

Mike Clark
  • 9,792
  • 2
  • 39
  • 50
  • 1
    @Simon it's a standard Microsoft pre-processor macro for dealing with creating programs that compile both in Unicode mode and ANSI mode. You can remove the _T() if you're not using [tchar.h and friends](https://msdn.microsoft.com/en-us/library/windows/desktop/ff381407(v=vs.85).aspx#tchars). – Mike Clark Feb 24 '17 at 21:45
7

OutputDebugString is the way to do it. Stack Overflow question How can I use the TRACE macro in non-MFC projects? contains information how to make something akin to MFC's TRACE macro using OutputDebugString.

Community
  • 1
  • 1
Necrolis
  • 25,222
  • 3
  • 61
  • 100
3

If you use C++, you may be interested on my portable TRACE macro.

#ifdef ENABLE_TRACE
#  ifdef _MSC_VER
#    include <windows.h>
#    include <sstream>
#    define TRACE(x)                           \
     do {  std::ostringstream s;  s << x;      \
           OutputDebugString(s.str().c_str()); \
        } while(0)
#  else
#    include <iostream>
#    define TRACE(x)  std::cerr << x << std::flush
#  endif
#else
#  define TRACE(x)
#endif

example:

#define ENABLE_TRACE  //can depend on _DEBUG or NDEBUG macros
#include "my_above_trace_header.h"

int main (void)
{
   int     i = 123;
   double  d = 456.789;
   TRACE ("main() i="<< i <<" d="<< d <<'\n');
}

Any improvements/suggestions/contributions are welcome ;-)

oHo
  • 46,029
  • 27
  • 151
  • 189