visual-studio 将stdout和stderr重定向到microsoft Visual Studio的输出调试控制台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2683249/
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
Redirect stdout and stderr to the output debug console of microsoft visual studio
提问by Arthur
I am using microsoft visual studio to do C++. I don't see std::err and std::out in the output console of the IDE. Is there a way to redirect them ?
我正在使用 Microsoft Visual Studio 来执行 C++。我在 IDE 的输出控制台中没有看到 std::err 和 std::out。有没有办法重定向它们?
回答by Brandon
You can indeed redirect std::out and std::err. Simply right click on your project in the solution explorer and select Properties. Then select Configuration Properties -> Debuggingand put the appropriate arguments into the Command Argumentsfield. For example, to redirect std::err to a file, I would type in 2> ErrorLog.txt.
您确实可以重定向 std::out 和 std::err。只需在解决方案资源管理器中右键单击您的项目并选择Properties。然后选择Configuration Properties -> Debugging适当的参数并将其放入该Command Arguments字段中。例如,要将 std::err 重定向到文件,我会输入2> ErrorLog.txt.
The things you type in Command Argumentssimply get appended as command line arguments when Visual Studio runs your program, just like you had manually typed them in to the console. So, the above example simply tells VisualStudio to run your program with the command <programName>.exe 2> ErrorLog.txtinstead of just <programName>.exe.
您键入的东西Command Arguments时,Visual Studio中运行你的程序,就像你曾经手动输入他们在控制台直接申请追加作为命令行参数。因此,上面的示例只是告诉 VisualStudio 使用命令<programName>.exe 2> ErrorLog.txt而不是<programName>.exe.
回答by Dan
I know this is an old thread but I can't help but giving the answer since I can't believe there still is no real answer. What you can do is redirect the coutto a ostringstreamof your choice. To do this, derive a new class from streambufthat will send the stream to OutputDebugString(Lets call this class OutputDebugStream) and create an instance of the class, myStream. Now call:
我知道这是一个旧线程,但我忍不住给出了答案,因为我无法相信仍然没有真正的答案。您可以做的是将cout重定向到您选择的ostringstream。要做到这一点,从streambuf派生一个新类,它将把流发送到OutputDebugString(让我们调用这个类OutputDebugStream)并创建类的一个实例myStream。现在调用:
cout.rdbuf(&myStream)
I used coutfor an example. This same technique can be used with cerr, just call
我以cout为例。同样的技术可以与cerr一起使用,只需调用
cerr.rdbuf(&myStream).
Stdoutis a little more difficult if you are not using cout. You can redirect stdoutduring runtime by using freopen(), but it must be to a file. To get this to redirect to the Debug screen is a little more difficult. One way is to use fmemopen()if it is available (it is not standard) and write a streambufto output this data to the Debug screen. Alternatively, you can redirect to a file and write a stream to open as input and redirect to the debug stream. A bit more work, but I think it is possible.
标准输出,如果你不使用是有点难度COUT。您可以在运行时使用重定向标准输出freopen(),但它必须重定向到文件。让它重定向到调试屏幕有点困难。一种方法是使用fmemopen()它是否可用(它不是标准的)并编写一个流缓冲来将此数据输出到调试屏幕。或者,您可以重定向到一个文件并写入一个流作为输入打开并重定向到调试流。需要做更多的工作,但我认为这是可能的。
回答by ejectamenta
I use the following macro for output to the visual studio console
我使用以下宏输出到 Visual Studio 控制台
#ifdef _MSC_VER
#include <Windows.h>
#include <iostream>
#include <sstream>
#include <opencv/cxcore.h>
#define DBOUT( s ) \
{ \
std::wostringstream os_; \
os_ << s; \
OutputDebugStringW( os_.str().c_str() ); \
}
#else
#define DBOUT( s ) \
{ \
std::cout << s; \
}
#endif
Now if I could only get it to work within a cuda kernel?!
现在,如果我只能让它在 cuda 内核中工作?!

