从mingw上的WinAPI gui程序打开控制台:在此作用域中未声明'_fdopen' [英] Opening console from WinAPI gui program on mingw: '_fdopen' was not declared in this scope

查看:100
本文介绍了从mingw上的WinAPI gui程序打开控制台:在此作用域中未声明'_fdopen'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在MinGW下开发WinAPI Gui应用程序.在调试版本中,我想打开控制台并将stdin/stdout流重定向到它,因此我可以看到诊断消息正在调试中打印.我关注了这篇文章: http://justcheckingonall.wordpress.com/2008/08/29/console-window-win32-app/

I am currently working on WinAPI Gui application under MinGW. In debug version I want to open console and redirect stdin/stdout streams to it, so I can see diagnostic messages being printed in debug. I followed this article: http://justcheckingonall.wordpress.com/2008/08/29/console-window-win32-app/

它在Visual Studio下工作,但是在MinGW上编译时,即使包含stdio.h,它也会吐出此消息:

It works under Visual Studio, but when compiled on MinGW it spits this message, even if stdio.h is included:

error: '_fdopen' was not declared in this scope

MinGW的参数:

mingw32-g++.exe -march=pentium4 -std=c++11 -w -fpermissive -fno-strict-aliasing -D__STDC_CONSTANT_MACROS -D_WINDOWS -DUNICODE -D_UNICODE -g -D_DEBUG

我在Google上搜索了很多东西,这似乎是MinGW中的错误,如果使用C ++ 11,则标头中没有定义_fdopen.由于我依赖C ++ 11功能,因此无法将其关闭,因此我正在寻找替代方法-是否有任何方法可以在Windows上打开控制台并重定向不依赖于fdopen的stdin/stdout?如果没有,我的问题还有其他解决办法吗?

I've googled a lot and it seems to be a bug in MinGW, there is no _fdopen defined in header if C++11 is used. Since I rely on C++11 features, I cannot turn it off, so I am looking for alternatives - is there any way to open console on Windows and redirect stdin/stdout that does not rely on fdopen? If not, are there any other solutions to my problem?

我也尝试过手动声明_fdopen(或fdopen),但是没有通过链接阶段

I tried also to manually declare _fdopen (or fdopen), but then it didn't pass the linking phase

MinGW版本:4.7.1

MinGW version: 4.7.1

推荐答案

通过一些简短的研究,据我了解,您应该能够在GNU C中使用_fdopen/fdopen ++ 11具有正确的配置设置(即,通过启用POSIX功能),但是Windows实现中存在一个长期存在的错误.您是否可以直接解决此问题取决于您使用的是哪个运行时库.

From some brief research, it is my understanding that you should be able to use _fdopen/fdopen in GNU C++11 with the right configuration settings (i.e., by enabling POSIX functions) but that there is a long-standing bug in the Windows implementations. Whether you can directly work around this issue presumably depends on which runtime library you're using.

但是,根据情况,还有其他各种可能的解决方法:

However, there are various other potential workarounds depending on the scenario:

  • 在您的特定情况下,由于您是在构建时做出决定的,因此您可以简单地将调试版本构建为控制台应用程序,然后让Windows完成工作.

  • In your particular case, since you're making the decision at build time, you can simply build the debug version as a console application and let Windows do the work.

应该有可能(同样,取决于运行时库)来配置构建,以便在运行时库初始化之前运行您的一些代码;希望如果控制台已经存在,则运行时库将在初始化期间导入标准流.

It should be possible (again, depending on the runtime library) to configure the build so that some code of yours runs before the runtime library initialization; hopefully, if the console is already present the runtime library will import the standard streams during initialization.

您应该能够使用fopen打开CONIN$和/或CONOUT$,以便直接获取FILE对象,而不是使用GetStdHandle获取Windows句柄.这可能是最通用的解决方案.

You should be able to open CONIN$ and/or CONOUT$ using fopen so as to obtain FILE objects directly, rather than using GetStdHandle to obtain Windows handles. This is probably the most general solution.

这篇关于从mingw上的WinAPI gui程序打开控制台:在此作用域中未声明'_fdopen'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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