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

发布于 2024-08-28 15:08:38 字数 437 浏览 9 评论 0原文

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

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

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

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

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

您知道为什么没有显示任何内容吗?

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

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.

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

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

西瑶 2024-09-04 15:08:38

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

使用 SUBSYSTEM:CONSOLE 构建的程序预计是文本模式应用程序。对于这种类型的应用程序,stdout 和 stderr 打印到您启动它们的控制台,如有必要,创建一个新控制台。

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

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

YourApplication.exe > output.txt

或者如果您有 cat (或等效项):

YourApplication.exe | cat

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

On Windows, programs usually are built as either a SUBSYSTEM:WINDOWS application or as 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.

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 AllocConsole to 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.

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

YourApplication.exe > output.txt

or if you have cat (or an equivalent):

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

遇到 2024-09-04 15:08:38

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

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

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.

给不了的爱 2024-09-04 15:08:38

尝试

CONFIG += console 。

在 .pro 文件中

Try

CONFIG += console

in your .pro file.

放手` 2024-09-04 15:08:38

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

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 AllocConsole to create one.

相思故 2024-09-04 15:08:38

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文