在Visual C++ IDE中的输出窗口上打印输出

Car*_*ven 4 c++ visual-studio-2010

如何在Visual C++中的输出窗口上打印?我正在处理的项目不是控制台窗口项目类型.那是我构建并运行它时,它不会打开控制台窗口.相反,它会打开一个win32应用程序,它不是由我构建的.我只是在添加内容.

我是C++的新手,因为我无法在任何控制台上打印变量,这使我很难调试.

由于Visual Studio 2010项目在构建和运行时没有启动控制台,我是否仍然可以在IDE的"输出"窗口中打印变量等输出?

谢谢你的帮助.

Dar*_*ara 6

您可以使用OutputDebugString("...");打印到Visual Studio的"输出"窗口.你必须这样做#include <windows.h>.


oli*_*bre 5

我写了一个可移植的 TRACE 宏。
在 MS-Windows 上,它基于OutputDebugString其他答案所示的内容。

这里我分享一下我的工作:

#ifdef ENABLE_TRACE
#  ifdef _MSC_VER
#    include <windows.h>
#    include <sstream>
#    define TRACE(x)                           \
     do {  std::stringstream s;  s << (x);     \
           OutputDebugString(s.str().c_str()); \
        } while(0)
#  else
#    include <iostream>
#    define TRACE(x)  std::clog << (x)
#  endif        // or std::cerr << (x) << std::flush
#else
#  define TRACE(x)
#endif
Run Code Online (Sandbox Code Playgroud)

例子:

#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');
}
Run Code Online (Sandbox Code Playgroud)

请随时提供任何改进/建议/贡献;-)