我在VisualStudio2010中启动了一个空白项目来编写C应用程序。如何将调试信息发送到Output窗口(菜单debug->Windows->Output)?有没有一种相对简单的方法来实现TRACE
或OutputDebugString
或类似的东西?
您可以从VS C程序中使用OutputDebugString
。
#include <windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
OutputDebugString(_T("Hello Worldn"));
return 0;
}
只有使用调试(调试>开始调试)运行时,输出才会可见
在"输出"窗口中,选择"调试"作为"显示输出自:"
OutputDebugString
是实现它的方法。堆栈溢出问题如何在非MFC项目中使用TRACE宏包含如何使用OutputDebugString
制作类似于MFC的TRACE
宏的信息。
如果您使用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');
}
欢迎任何改进/建议/贡献;-)