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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7697842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:21:40  来源:igfitidea点击:

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

c++visual-studio-2010

提问by Carven

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 open a console window. Instead, it opens a win32 application, which isn't built by me. I am just adding things to it.

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

I am pretty new to C++ and because I couldn't print variables out on any console, it makes it very hard for me to debug.

我对 C++ 很陌生,因为我无法在任何控制台上打印变量,这让我很难调试。

Since the Visual Studio 2010 project doesn't launch console when I build and run it, can I still print outputs such as variables and others on the Output window of the IDE?

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

Thanks for any help.

谢谢你的帮助。

回答by Darcara

You can use OutputDebugString("...");to print to the Output window of Visual Studio. You have to #include <windows.h>though.

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

回答by olibre

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

我编写了一个便携式 TRACE 宏。
在 MS-Windows 上,它基于OutputDebugString其他答案所指示的那样。

Here I share my work:

在这里分享一下我的作品:

#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

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 ;-)

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

回答by Miguel

Instead of printing to the Output window in VS as indicated by other answers, I prefer to create a console window in my GUI apps, then use regular printf or cout to write debugging info to it. This has the benefit that you can do it even when you run without the debugger.

我更喜欢在我的 GUI 应用程序中创建一个控制台窗口,而不是打印到 VS 中的输出窗口,然后使用常规的 printf 或 cout 将调试信息写入其中。这样做的好处是,即使您在没有调试器的情况下运行也可以这样做。

See this sitefor a simple function that sets up a console.

有关设置控制台的简单功能,请参阅此站点

回答by FailedDev

I have used this in the past, although not with a win32 application. You could give it a shot though :)

我过去曾使用过它,尽管没有使用 win32 应用程序。不过你可以试一试:)

http://www.cplusplus.com/forum/lounge/17371/

http://www.cplusplus.com/forum/lounge/17371/

回答by MartinStettner

You can use the Windows function OutputDebugString(see here) to send output to debuggers. These outputs are shown in the VS output window. You can also watch these outputs with external applications, e.g. DebugView.

您可以使用 Windows 函数OutputDebugString(请参阅此处)将输出发送到调试器。这些输出显示在 VS 输出窗口中。您还可以使用外部应用程序查看这些输出,例如DebugView

Remember to remove these statements from your production code if you don't want other people to see these debug messages (which would otherwise be possible using tools like DebugView...)

如果您不希望其他人看到这些调试消息,请记住从您的生产代码中删除这些语句(否则可以使用 DebugView 之类的工具...)