Printing output on the Output Window in Visual C++ IDE

前端 未结 5 1741
生来不讨喜
生来不讨喜 2021-01-06 12:38

How do I print on the output window in Visual C++? The project that I am working on isn\'t of a console window project type. That\'s when I build and run it, it doesn\'t ope

5条回答
  •  甜味超标
    2021-01-06 12:55

    I have written a portable TRACE macro.
    On MS-Windows, it is based on OutputDebugString as indicated by other answers.

    Here I share my work:

    #ifdef ENABLE_TRACE
    #  ifdef _MSC_VER
    #    include 
    #    include 
    #    define TRACE(x)                           \
         do {  std::stringstream s;  s << (x);     \
               OutputDebugString(s.str().c_str()); \
            } while(0)
    #  else
    #    include 
    #    define TRACE(x)  std::clog << (x)
    #  endif        // or std::cerr << (x) << std::flush
    #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     v1 = 123;
       double  v2 = 456.789;
       TRACE ("main() v1="<< v1 <<" v2="<< v2 <<'\n');
    }
    

    Please feel free to give any improvements/suggestions/contributions ;-)

提交回复
热议问题