使用 MinGW 的 Eclipse CDT 不在控制台中输出

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

Eclipse CDT using MinGW does not output in console

eclipseconsolemingweclipse-cdt

提问by Guillaume

I have a Windows 7 64-bit PC and I am trying to install a free C++ IDE, so I chose to install Eclipse Helios with CDT.

我有一台 Windows 7 64 位 PC,我想安装一个免费的 C++ IDE,所以我选择安装带有 CDT 的 Eclipse Helios。

For g++, make and gdb I installed msys and mingw according to this tutorial: http://wiki.wxwidgets.org/HowTo:_Install_MSYS_and_MinGW_for_use_with_Eclipse_CDT

对于 g++、make 和 gdb,我根据本教程安装了 msys 和 mingw:http://wiki.wxwidgets.org/HowTo:_Install_MSYS_and_MinGW_for_use_with_Eclipse_CDT

The versions are:

版本是:

  • make: GNU make 3.81
  • g++: 4.5.0
  • gdb: 7.1
  • 制作:GNU make 3.81
  • g++:4.5.0
  • gdb:7.1

So it should be compatible with my 64-bit CPU.

所以它应该与我的 64 位 CPU 兼容。

However when I try to run a very simple program with eclipse, I see nothing on the console. The source code is:

但是,当我尝试使用 eclipse 运行一个非常简单的程序时,我在控制台上什么也看不到。源代码是:

#include <iostream>

 using namespace std;

 int main()
 {
    int i;

    cout << "Enter an integer: " << endl;
    cin >> i;
    cout << endl << "i is " << i << endl;

    return 0;
 }

The build is fine and when I launch the .exe with command (windows console) the behavior is as expected. But with the Eclipse console I see nothing with run and with debug the output is just: "Enter an integer: ", then when I type in a number and hit enter it does nothing.

构建很好,当我使用命令(Windows 控制台)启动 .exe 时,行为符合预期。但是在 Eclipse 控制台中,我在运行和调试时看不到任何内容,输出只是:“输入一个整数:”,然后当我输入一个数字并按回车键时,它什么也不做。

Does anyone know how to fix this please?

有谁知道如何解决这个问题?

Thanks,

谢谢,

Guillaume

纪尧姆

PS: I use the toolchain "Linux GCC", with "MinGW GCC" I have nothing at all in the console.

PS:我使用工具链“Linux GCC”,使用“MinGW GCC”我在控制台中什么都没有。

采纳答案by guest

This console bug has been noticed in 64-bit versions of eclipse:

在 64 位版本的 eclipse 中已注意到此控制台错误:

http://www.eclipse.org/forums/index.php?t=msg&th=197552&start=0&S=2a2b64e1f1404705c0214976bd477428

http://www.eclipse.org/forums/index.php?t=msg&th=197552&start=0&S=2a2b64e1f1404705c0214976bd477428

A workaround is to install the 32-bit eclipse

解决方法是安装 32 位 eclipse

回答by ossys

This worked for me on 64-bit install of Eclipse on Windows 7 using MinGW:

这对我使用 MinGW 在 Windows 7 上安装 Eclipse 的 64 位有效:

Right-click on your project. Select "Properties".

右键单击您的项目。选择“属性”。

Select the "Run/Debug Settings" Property on the left of the new window.

选择新窗口左侧的“运行/调试设置”属性。

In the right window, click on your executable to highlight (ie - Test.exe) and click "Edit".

在右侧窗口中,单击要突出显示的可执行文件(即 - Test.exe),然后单击“编辑”。

In the Environment tab, hit "New"

在环境选项卡中,点击“新建”

Name: PATH
Value: Path to your MinGW bin directory. (For me this was: C:\devcore\MinGW\bin)

名称:PATH
值:MinGW bin 目录的路径。(对于我来说,这是:C:\devcore\MinGW\bin

Click "OK" on all windows to close down.

在所有窗口上单击“确定”关闭。

Try running again, it should print output to the screen.

再次尝试运行,它应该将输出打印到屏幕上。

回答by Dhiral Pandya

You need to set up linker I am using MinGW.

您需要设置链接器,我正在使用 MinGW。

Follow below steps.

请按照以下步骤操作。

Goto Project > Properties > C/C++ Build > Settings > Tool Settings (Tab) > MinGW C++ Linker (Option) > Add Command (g++ -static-libgcc -static-libstdc++)   (default command is only g++)

回答by Yifu

purlogic's solution works. Instead to set that for every project, I found it can be set globally:

purlogic 的解决方案有效。而不是为每个项目设置它,我发现它可以全局设置:

In Window -> Preferences-> C/C++ -> Build -> Environment Add a variable for your compiler. e.g, I added: MINGW, with value "C:\MinGW\bin"

在 Window -> Preferences-> C/C++ -> Build -> Environment 为你的编译器添加一个变量。例如,我添加了:MINGW,值为“C:\MinGW\bin”

回答by michael_s

I ran into the same problem, because of multiple gcc installations on one PC. But Greg's solutiononly worked partly for me.

我遇到了同样的问题,因为在一台 PC 上安装了多个 gcc。但是格雷格的解决方案只对我有用。

In my case the flush was not done in the application explicitly. While C++ programs often use std::cout << ... << std::endlwhere the endldoes a flush, my program used actual C-output such as the usual printf. The printfcould be seen directly when starting the program in the cmd-window. However in eclipse console they were missing. Hence a

在我的情况下,刷新没有在应用程序中明确完成。虽然 C++ 程序经常使用std::cout << ... << std::endlwhereendl进行刷新,但我的程序使用了实际的 C 输出,例如通常的printf. 在printf开始在cmd窗口程序时,可以直接看到。但是在 Eclipse 控制台中,它们丢失了。因此一个

fflush(stdout);

after the printfdid the thing for me. That could be an issue within the eclipse console implementation. I guess that's why fixing the Path did not work for some people here.

printf为我做了这件事之后。这可能是 eclipse 控制台实现中的一个问题。我想这就是为什么修复 Path 对这里的某些人不起作用的原因。

An alternative solution instead of setting the PATH within the "Run" settings is to start the whole eclipse using a batch file, which looks essentially like this:

替代在“运行”设置中设置 PATH 的另一种解决方案是使用批处理文件启动整个 eclipse,它基本上如下所示:

set PATH=<mymingwlocation>\bin;%PATH%
start <myeclipselocation>\eclipse.exe

Then any run configuration would use the correct MingW location by default. That might also fix other problems that could arise from using the wrong gcc.

然后任何运行配置都会默认使用正确的 MingW 位置。这也可能解决因使用错误的 gcc 而可能出现的其他问题。

回答by JoeManiaci

Had this issue on 64-/32-bit eclipse Kepler CDT to work on a openCV/wxWidgets tool, Win7, using MinGW to build.

在 64 位/32 位 Eclipse Kepler CDT 上遇到此问题,以便在使用 MinGW 构建的 openCV/wxWidgets 工具 Win7 上工作。

If anyone comes across this while having this issue and are working with openCV you will already know that there are many outdated openCV building/installing instructions all over the internet.

如果有人在遇到此问题并使用 openCV 时遇到此问题,您就会知道互联网上有许多过时的 openCV 构建/安装说明。

One I had was to go to Build Settings -> Linker -> Miscellaneous and inside of the Linker Flags text entry box, type in -Wl,--subsystem,windows -mwindowsHowever, this disables cout from outputting to a command line terminal in windows.

我必须去构建设置 -> 链接器 -> 杂项,在链接器标志文本输入框内,输入 -Wl,--subsystem,windows -mwindows但是,这会禁用 cout 输出到 Windows 中的命令行终端。

Doing some more digging this looks intentional, apparently the -mwindows involves directing STDOUT away from a command line specifically to a GUI-like application.

做更多的挖掘这看起来是有意的,显然 -mwindows 涉及将 STDOUT 从命令行直接定向到类似 GUI 的应用程序。

Also, removing -mwindowsand just leaving in -Wl,--subsystem,windowsaccomplishes the task of redirecting STDOUT anyway away from the command line all the same.

此外,删除-mwindows并只留下来-Wl,--subsystem,windows完成重定向 STDOUT 的任务,无论如何都远离命令行。

Now mind you, I haven't built up anything yet outside of a hello world program involving wxWidgets and openCV, so I am not at the point of doing a cout into a part of a GUI so I don't know if that functionality would now be broken or if it would print out to the GUI object, as well as a command line terminal.

现在请注意,除了涉及 wxWidgets 和 openCV 的 hello world 程序之外,我还没有构建任何东西,所以我不打算对 GUI 的一部分进行 cout,所以我不知道该功能是否会现在坏了,或者它是否会打印到 GUI 对象以及命令行终端。

回答by Roberto

Have you tried to execute the eclipse.exe with administrator privileges ?? it worked for me !

您是否尝试过以管理员权限执行 eclipse.exe ?它对我有用!

回答by zaursh

Add PATH variable (PATH="your MinGW/bin directory path") into your C++ project by Run -> Run Configurations ->in Environment Tab

通过运行 -> 运行配置 -> 在环境选项卡中将 PATH 变量(PATH="您的 MinGW/bin 目录路径")添加到您的 C++ 项目中

enter image description here

在此处输入图片说明

回答by rmk

Or set the linker option -static. Works for me at least.

或者设置链接器选项-static。至少对我有用。