请参阅.exe的std :: cout [英] See std::cout of .exe
问题描述
我有一个在Windows上编译的.exe文件.但是,当我从命令行运行它时,我看不到程序输出到std :: cout或std :: cerr的任何内容.该程序会运行并继续运行,即使在命令行返回到提示符后也是如此(这是一个GUI程序,直到我按下退出按钮后才退出).如何查看程序的输出?
I have a .exe file that I've compiled on windows. But when I run it from the command line I don't see any of the things my program outputs to std::cout or std::cerr. The program runs and continues to run even after the command line returns to the prompt (it's a GUI program and doesn't quit until I press the quit button). How can I see the output of my program?
我正在使用cmake制作一个Visual Studio项目,然后使用msbuild进行编译.
I'm using cmake to make a visual studio project which I then compile with msbuild.
推荐答案
最简单的方法是将程序重建为控制台应用程序. link.exe
的选项必须是/SUBSYSTEM:CONSOLE
而不是/SUBSYSTEM:WINDOWS
;大概有一种直接的方法可以在cmake中指定它.
The simplest approach is to rebuild the program as a console application. The option to link.exe
needs to be /SUBSYSTEM:CONSOLE
instead of /SUBSYSTEM:WINDOWS
; presumably there is a straightforward way of specifying this in cmake.
此更改完全不会影响您的GUI,但是如果该进程尚未与之关联,它将导致Windows分配一个控制台.此外,命令行外壳程序通常会等待控制台应用程序退出后再继续.
This change shouldn't affect your GUI at all, but it will cause Windows to allocate a console if the process isn't already associated with one. Also, command line shells will usually wait for console applications to exit before continuing.
另一种方法是调用 AllocConsole
显式创建新控制台,或 AttachConsole
如果要使用现有的.或者,当然,您可以将输出发送到日志文件.
The other approach is to call AllocConsole
to explicitly create a new console, or AttachConsole
if you want to use an existing one. Or, of course, you could send the output to a log file.
其他
根据Google搜索,您可以通过在源代码中添加以下行来将该程序构建为控制台应用程序:
According to a Google search, you can build the program as a console application by adding the following line to your source code:
#pragma comment(linker, "/SUBSYSTEM:CONSOLE")
这可能是最简单的解决方案.如果只希望控制台进行调试构建,则可以将其放在#if
块中.
This is probably the easiest solution. You can put it in an #if
block if you only want the console for debug builds.
另请参见 CMake:如何使用不同的ADD_EXECUTABLE用于调试版本?
这篇关于请参阅.exe的std :: cout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!