在非控制台应用程序项目中的c ++控制台 [英] c++ console in a non-console application project

查看:175
本文介绍了在非控制台应用程序项目中的c ++控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++中的Windows应用程序,我想使用 cout 进行调试。

看起来 cout 的效果是否隐藏,是否有一个配置选项允许使用调试信息打开控制台窗口?

I'm working on a windows application in C++, and I'd like to use cout for debugging purposes.
It seems that the effects of cout are hidden, is there a configuration option that will allow a console window to open with my debug information?

推荐答案

你有几个合理的选择。一个是将你的入口点从 WinMain 重命名为 main ,因此它将被构建为控制台应用程序。这仍然可以使用 RegisterClass CreateWindow 很好。

You have a couple of reasonable options. One is to rename your entrypoint from WinMain to main, so it'll be built as a console application. That can still use things like RegisterClass and CreateWindow just fine.

显而易见的替代方法是使用 OutputDebugString 而不是 cout 。它的输出将显示在调试器中,或任何数量的专门用于显示它产生的程序(基本上是不提供其他调试功能的微调试器)。

The obvious alternative would be to use OutputDebugString instead of cout. The output from that will show up in a debugger, or any number of programs built specifically to display what it produces (basically mini-debuggers that just don't provide other debugging capabilities).

我认为不太合理的第三个解决方案是调用 AllocConsole 分配一个控制台,然后写入它。如果你不介意使用Windows本地控制台函数( WriteFile WriteConsoleOutput 等)没关系。不幸的是,它不会初始化C ++流与控制台一起使用,因此您需要打开指向控制台的文件流,或者找到并使用(未记录的)函数初始化控制台的流库。虽然这可以工作,你最终做额外的工作,以编写非便携式代码,和很少(如果有)的优势,弥补它(在这种情况下,非便携式可以包括甚至不工作其他版本的同一个编译器,至少如果你调用库的初始化函数)。

A third solution I'd consider less reasonable is to call AllocConsole to allocate a console, then write to it. If you don't mind doing all your writing with Windows-native console functions (WriteFile, WriteConsoleOutput, etc.) that's fine. Unfortunately, it won't initialize the C++ streams to work with the console, so you'll need to either open file streams pointing the console, or else find and use the (undocumented) function that initializes the streams library for the console. While this can be made to work, you end up doing extra work to write non-portable code, and little (if any) advantage to make up for it (and in this case, "non-portable" can include not even working with other versions of the same compiler, at least if you invoke the library's initialization function).

这篇关于在非控制台应用程序项目中的c ++控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆