C++ 什么可以解释 std::cout 不显示任何内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2627900/
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 00:08:53  来源:igfitidea点击:

What can explain std::cout not to display anything?

c++visual-studioqtqt-creatorcout

提问by Beno?t

For whatever reason, std::cout does not display anything with my application. The description of my development environment follows.

无论出于何种原因, std::cout 都不会在我的应用程序中显示任何内容。我的开发环境的描述如下。

I am working on a Qt application using Qt Creator. Since Qt Creator can't be launched from my station (XP64), i am currently developping it with Visual Studio 2008 and the Qt plugin (by importing the .pro project file). Everything seems fine and the application works.

我正在使用 Qt Creator 开发 Qt 应用程序。由于无法从我的工作站 (XP64) 启动 Qt Creator,我目前正在使用 Visual Studio 2008 和 Qt 插件(通过导入 .pro 项目文件)开发它。一切似乎都很好,应用程序可以正常工作。

In some cases (depending on command line arguments), i don't want to launch the HIM, just to display a few sentences in the CLI (command line required arguments, for instance).

在某些情况下(取决于命令行参数),我不想启动 HIM,只是为了在 CLI 中显示几句话(例如,命令行需要参数)。

I don't get any error, but nothing is displayed. The corresponding code, which i am sure is run is the (classical) following :

我没有收到任何错误,但没有显示任何内容。我确定运行的相应代码是(经典)以下代码:

std::cout << "is this going to be displayed ?" << std::endl;

Do you have any idea why nothing is displayed ?

你知道为什么什么都不显示吗?

回答by jamesdlin

On Windows, programs usually are built as either a SUBSYSTEM:WINDOWS application or as SUBSYSTEM:CONSOLE.

在 Windows 上,程序通常构建为SUBSYSTEM:WINDOWS 应用程序或 SUBSYSTEM:CONSOLE

Programs built with SUBSYSTEM:CONSOLE are expected to be text-mode applications. For this type of application, stdout and stderr print to the console that you launched them from, creating a new console if necessary.

使用 SUBSYSTEM:CONSOLE 构建的程序应该是文本模式的应用程序。对于此类应用程序,stdout 和 stderr 会打印到您从中启动它们的控制台,并在必要时创建一个新控制台。

In contrast, SUBSYSTEM:WINDOWS applications do not bother with a console. You can still write to stdout and stderr, but they normally don't go anywhere. You could use AllocConsoleto create a console to print to, but this will always print to a newly created console window, not to a console window you launched the program from.

相比之下,SUBSYSTEM:WINDOWS 应用程序不需要控制台。您仍然可以写入 stdout 和 stderr,但它们通常不会去任何地方。您可以使用AllocConsole创建要打印到的控制台,但这将始终打印到新创建的控制台窗口,而不是您从中启动程序的控制台窗口。

One trick for SUBSYSTEM:WINDOWS applications is that even though there's no console, you can still pipestdout and stderr. To pipe stdout, you can do:

SUBSYSTEM:WINDOWS 应用程序的一个技巧是,即使没有控制台,您仍然可以通过管道传输 stdout 和 stderr。要管道标准输出,您可以执行以下操作:

YourApplication.exe > output.txt

or if you have cat(or an equivalent):

或者如果你有cat(或同等学历):

YourApplication.exe | cat

Also note that there's not really any difference between SUBSYSTEM:WINDOWS applications and SUBSYSTEM:CONSOLE applications other than how Windows treats them when creating the process. (You can create windows in SUBSYSTEM:CONSOLE applications.) You therefore can easily switch between SUBSYSTEM types(for example, to use SUBSYSTEM:CONSOLE for debug builds and SUBSYSTEM:WINDOWS for release ones).

另请注意,除了 Windows 在创建进程时如何处理它们之外,SUBSYSTEM:WINDOWS 应用程序和 SUBSYSTEM:CONSOLE 应用程序之间没有任何区别。(您可以在 SUBSYSTEM:CONSOLE 应用程序中创建窗口。)因此您可以轻松地在 SUBSYSTEM 类型之间切换(例如,使用 SUBSYSTEM:CONSOLE 进行调试构建,使用 SUBSYSTEM:WINDOWS 进行发布构建)。

回答by Beno?t

Ok, answer found. Simple answer, of course, as always when encountering such problems. Michael Aaron was on the right tracks.

好的,找到答案了。简单的回答,当然,遇到此类问题时一如既往。迈克尔·亚伦走在正确的轨道上。

Simply changing SubSystem to Console in project configuration (/Configuration properties/Linker/System) makes the whole thing work. The GUI still works, but with a background console. I can deal with that.

只需在项目配置(/配置属性/链接器/系统)中将 SubSystem 更改为 Console 即可使整个工作正常进行。GUI 仍然有效,但带有后台控制台。我可以处理。

回答by ktk

Try

尝试

CONFIG += console

配置 += 控制台

in your .pro file.

在您的 .pro 文件中。

回答by Michael Aaron Safyan

Windows distinguishes between console applications and GUI applications, and does not create a console for GUI applications, by default (see this page from MSDN). You can use AllocConsoleto create one.

默认情况下,Windows 区分控制台应用程序和 GUI 应用程序,并且不会为 GUI 应用程序创建控制台(请参阅 MSDN 中的此页面)。您可以使用AllocConsole创建一个。

回答by manuel

Perhaps it's not the std::cout line that makes it not displayed, but the function containing it. Maybe it's not invoked at all, and that's why std::cout doesn't work.

也许不是 std::cout 行使其不显示,而是包含它的函数。也许它根本没有被调用,这就是 std::cout 不起作用的原因。