【发布时间】:2012-03-21 09:17:17
【问题描述】:
我在Visual Studio 2010 开始了一个空白项目来编写一个 C 应用程序。如何将调试信息发送到 Output 窗口(菜单 Debug -> Windows -> Output )?有没有相对简单的方法来实现TRACE 或OutputDebugString 或类似的东西?
【问题讨论】:
标签: c visual-studio-2010 output-window debug-information
我在Visual Studio 2010 开始了一个空白项目来编写一个 C 应用程序。如何将调试信息发送到 Output 窗口(菜单 Debug -> Windows -> Output )?有没有相对简单的方法来实现TRACE 或OutputDebugString 或类似的东西?
【问题讨论】:
标签: c visual-studio-2010 output-window debug-information
如果你使用 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');
}
欢迎任何改进/建议/贡献;-)
【讨论】:
OutputDebugString 是 的方法。 Stack Overflow 问题 How can I use the TRACE macro in non-MFC projects? 包含如何使用 OutputDebugString 制作类似于 MFC 的 TRACE 宏的信息。
【讨论】:
您可以在 VS C 程序中使用OutputDebugString。
#include <windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
OutputDebugString(_T("Hello World\n"));
return 0;
}
只有在运行调试时才能看到输出(调试 > 开始调试)
在“输出”窗口中,为“显示输出自:”选择“调试”
【讨论】: