【问题标题】:Simple way to send debug information to the Visual Studio 'Output' window将调试信息发送到 Visual Studio“输出”窗口的简单方法
【发布时间】:2012-03-21 09:17:17
【问题描述】:

我在Visual Studio 2010 开始了一个空白项目来编写一个 C 应用程序。如何将调试信息发送到 Output 窗口(菜单 Debug -> Windows -> Output )?有没有相对简单的方法来实现TRACE 或OutputDebugString 或类似的东西?

【问题讨论】:

标签: c visual-studio-2010 output-window debug-information


【解决方案1】:

如果你使用 C++,你可能会对我的可移植 TRACE 宏感兴趣。

#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

示例:

#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');
}

欢迎任何改进/建议/贡献;-)

【讨论】:

    【解决方案2】:

    OutputDebugString 是 的方法。 Stack Overflow 问题 How can I use the TRACE macro in non-MFC projects? 包含如何使用 OutputDebugString 制作类似于 MFC 的 TRACE 宏的信息。

    【讨论】:

      【解决方案3】:

      您可以在 VS C 程序中使用OutputDebugString。

      #include <windows.h>
      
      int _tmain(int argc, _TCHAR* argv[])
      {
          OutputDebugString(_T("Hello World\n"));
          return 0;
      }
      

      只有在运行调试时才能看到输出(调试 > 开始调试)

      在“输出”窗口中,为“显示输出自:”选择“调试”

      【讨论】:

      • @Simon 它是一个标准的 Microsoft 预处理器宏,用于处理创建在 Unicode 模式和 ANSI 模式下编译的程序。如果您不使用 tchar.h and friends,则可以删除 _T()。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      相关资源
      最近更新 更多