使用 Windows 和 MinGW 在 Eclipse 中编写的 C++ 程序无法将输出显示到控制台视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5959908/
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
C++ program written in Eclipse using Windows and MinGW cannot display output to console view
提问by Gqqnbig
I'm using Windows 7 64bit.
我正在使用 Windows 7 64 位。
I installed eclipse version 3.6.2, cdt, and MinGW. I have a C++ console program in Eclipse as follows:
我安装了 Eclipse 版本 3.6.2、cdt 和 MinGW。我在 Eclipse 中有一个 C++ 控制台程序,如下所示:
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
setbuf(stdout, NULL);
for (int i = 0; i < 10000000; i++) {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
}
int val;
cin >> val;
return 0;
}
If I run this console program, it should display Hello worldto Console View in Eclipse, but nothing displays.
如果我运行这个控制台程序,它应该显示Hello world在 Eclipse 中的控制台视图中,但没有显示任何内容。
If I go to the debug folder and run the exe, it does print to the console.
如果我转到调试文件夹并运行 exe,它会打印到控制台。
If I make some syntax mistake, then the Eclipse Console View will show something, such as:
如果我犯了一些语法错误,那么 Eclipse 控制台视图将显示一些内容,例如:
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -osrc\hh.o ..\src\hh.cpp
..\src\hh.cpp: In function 'int main()':
..\src\hh.cpp:17:3: error: expected ';' before 'return'
Build error occurred, build is stopped
Time consumed: 255 ms.
Why is nothing showing in the Eclipse console view and how can I make my C++ console program display output?
为什么 Eclipse 控制台视图中没有显示任何内容,如何使我的 C++ 控制台程序显示输出?
采纳答案by Gqqnbig
I find the reason, just because I'm using 64bit eclipse!
我找到原因了,只是因为我用的是64位的eclipse!
I turn to 32 bit eclipse, the same code works fine.
我转向 32 位 eclipse,相同的代码工作正常。
回答by Vikyboss
I found a workaround from this site: http://www.eclipse.org/forums/index.php?=42e862594001fa4469bbc834885d545f&t=msg&th=197552
我从这个站点找到了一个解决方法:http: //www.eclipse.org/forums/index.php?=42e862594001fa4469bbc834885d545f&t=msg&th =197552
At that link, look at the reply from "No real Name".
在该链接上,查看“No real Name”的回复。
In case the link goes down, here is the content:
如果链接失效,以下是内容:
Environment: jdk1.6u18 64bit + Eclipse Helios 64bit + win7 64bit
No console output at "Run", but output correctly at "Debug".
The following method worked for me:
1. Goto Project->Properties->Run/Debug Settings, choose the .exe file
and press "Edit"
2. In the "Environment" tag, press "New", set it as:
"Name:PATH"
"Value:C:\MinGW\bin"
In fact, I have already set "C:\MinGW\bin" in windows PATH environment
variable, but it seemed to not work.
回答by Ales Chlubny
The problem is that your program uses dll libraries from MinGW - try to start the exe file manually, it will report some error about missing dlls.
问题是你的程序使用了 MinGW 的 dll 库 - 尝试手动启动 exe 文件,它会报告一些关于缺少 dll 的错误。
Solution can be, that you copy required dlls to .exe file in your project directory (and Release or Debug subdirectory, depends on what .exe are you executing with Run command).
解决方案可以是,您将所需的 dll 复制到项目目录中的 .exe 文件(以及 Release 或 Debug 子目录,取决于您使用 Run 命令执行的 .exe 文件)。
Or, in menu Run -> Run Configurationselect the configuration which you use for that .exe file (or create a new C/C++ Application configuration) and select Environment tab in the right panel. There add new variable named PATHwith value c:\MinGW\bin(this is default path to mingw\bin directory, use a path valid for your instalation if it's somewhere else)
Edit: Now I'm looking at post by Vikyboss and it's in fact the same - setting the PATH variable in Run configuration. Setting PATH variable in Preferences > C/C++ (Expand it) > Environmentas described by Sydraps didn't work for me.
或者,在菜单Run -> Run Configuration 中选择用于该 .exe 文件的配置(或创建新的 C/C++ 应用程序配置),然后在右侧面板中选择 Environment 选项卡。添加名为PATH且值为c:\MinGW\bin 的新变量(这是 mingw\bin 目录的默认路径,如果它在其他地方,请使用对您的安装有效的路径)
编辑:现在我正在查看 Vikyboss 的帖子,它是事实上相同 - 在运行配置中设置 PATH 变量。Sydraps 描述的在Preferences > C/C++ (Expand it) > Environment 中设置 PATH 变量对我不起作用。
But I think that static linking that libraries may be the best solution for you. In menu Project -> Propertiesselect C/C++ Build -> Settings. In the right panel select Configuration which you want to change (you may select All). In the tab Tool Settingsselect MinGW C++ Linker -> Miscellaneousand in the right panel in the Linker flagstype -static. Now the .exe will be bloated by the size of the libraries (in my case approx. +900kB for Hello world example, requiring 2 dlls), but it will be independent at any libraries.
但我认为静态链接该库可能是您的最佳解决方案。在菜单Project -> Properties 中选择C/C++ Build -> Settings。在右侧面板中选择要更改的配置(您可以选择全部)。在选项卡工具设置中选择MinGW C++ Linker -> Miscellaneous并在右侧面板中的Linker flagstype -static。现在 .exe 将因库的大小而膨胀(在我的情况下,Hello world 示例大约为 +900kB,需要 2 个 dll),但它在任何库中都是独立的。
I hope this will be helpful for anyone trying to start with Eclipse C/C++ and wondering why there's no Hello world in console. Ales Chlubny
我希望这对任何尝试使用 Eclipse C/C++ 并想知道为什么控制台中没有 Hello world 的人都会有所帮助。啤酒花
回答by Sydraps
I fixed the problem on my windows 7 x64 PC. In the Eclipse window go to Preferences > C/C++ (Expand it) > Environment > Add:
我在 Windows 7 x64 PC 上解决了这个问题。在 Eclipse 窗口中,转到 Preferences > C/C++ (Expand it) > Environment > Add:
"Name:PATH"
"Value:C:\MinGW\bin"
If this does not fix it. Try adding the above to the system environment variables on your PC in Computer > Advanced System settings
如果这不能解决它。尝试将上述内容添加到您 PC 上的计算机 > 高级系统设置中的系统环境变量中
回答by Jipson
My problem with displaying hello world(64 bit Windows7) in console was solved when I ran eclipse as administrator.
当我以管理员身份运行 eclipse 时,我在控制台中显示 hello world(64 位 Windows7)的问题得到了解决。
I added the C:\MinGW\binto environment variable path and then started eclipse as administrator
我添加C:\MinGW\bin到环境变量路径,然后以管理员身份启动 eclipse
回答by Tod
I created a Hello World C++ Project (MinGW GCC) app from the Eclipse wizard, cut and pasted your code and it compiled fine. Then prior to running I reduced your loop to 10 and it ran fine.
我从 Eclipse 向导创建了一个 Hello World C++ 项目 (MinGW GCC) 应用程序,剪切并粘贴了你的代码,它编译得很好。然后在运行之前我将您的循环减少到 10 并且运行良好。
You don't say how you created your project but it would appear likely your missing some include directive or library path. Also I don't see the need to include or setbuf(stdout, NULL). I also find it helpful when troubleshooting to NOT bring an entire namespace into scope. Rather do this: using std::cout; using std::cin;
您没有说明您是如何创建项目的,但您可能会丢失一些包含指令或库路径。此外,我认为不需要包含或 setbuf(stdout, NULL)。我还发现在故障排除时不要将整个命名空间纳入范围很有帮助。而是这样做:使用 std::cout; 使用 std::cin;
Finally, flushing the buffer each time with << endl; seems like overkill, adding a simple \n to the end of the string would be more efficient.(I did all this on Win 7 64 bit - but I was using a 32 bit version of Eclipse Galileo)
最后,每次使用 << endl; 刷新缓冲区;看起来有点矫枉过正,在字符串的末尾添加一个简单的 \n 会更有效。(我在 Win 7 64 位上完成了所有这些 - 但我使用的是 32 位版本的 Eclipse Galileo)
回答by user2840033
If you are using MinGW compiler,
如果您使用的是 MinGW 编译器,
Add
-static-libgcc -static-libstdc++as Linker flags for your new project. This text should be added to the Linker flags field, which can be found by right-clicking on the new Project in the Project Explorer and clicking on Properties. Under the Project Properties, expand the C/C++ Build menu and click on Settings. Under the Tool Settings tab, expand the MinGW C++ Linker menu and click on Miscellaneous. Add the text to the Linker flags field, then click the Apply button.
添加
-static-libgcc -static-libstdc++为新项目的链接器标志。此文本应添加到链接器标志字段中,可以通过右键单击项目资源管理器中的新项目并单击属性来找到该字段。在 Project Properties 下,展开 C/C++ Build 菜单并单击 Settings。在 Tool Settings 选项卡下,展开 MinGW C++ Linker 菜单并单击 Miscellaneous。将文本添加到链接器标志字段,然后单击应用按钮。
回答by nbout
Works for me by going Administator on Eclipse Kepler 64bit.
通过在 Eclipse Kepler 64 位上转到管理员来为我工作。
回答by pablo
In case someone is interested, I found how to fix it forever on Windows XP (may work in other windows version though) without you having to specify the variables of each executable and such:
如果有人感兴趣,我找到了如何在 Windows XP 上永久修复它(尽管可以在其他 Windows 版本中工作),而无需指定每个可执行文件的变量等:
Start Menu >RightClickon MyComputer >Properties >Advanced Options >Environment Variables
开始菜单>右击在我的电脑>属性>高级选项>环境变量
There, in the "User variables" field,
在那里,在“用户变量”字段中,
- If there's an environment variable called PATH, select Modify and add this information in the end of the Valuefield, separated by semicolon:
- 如果有一个名为 PATH 的环境变量,请选择修改并在值字段的末尾添加此信息,以分号分隔:
C:\MinGW\bin
Example: C:\SomeDirectory; C:\Another; C:\MinGW\bin
C:\MinGW\bin
示例:C:\SomeDirectory; C:\另一个; C:\MinGW\bin
- If the environment variable PATH does notexist, add a new one with this information:
- 如果环境变量 PATH不存在,请添加一个包含以下信息的新变量:
Name:PATH
Value:C:\MinGW\bin
名称:路径
值:C:\MinGW\bin
Accept all and you should get console output :)
接受所有,你应该得到控制台输出:)
回答by yohann.martineau
I had an issue with my eclipse-cdt, new C++ project > hello world (cygwin gcc), right click on exe file, run as and nothing was showing on console. It was the same with c project.
我的 eclipse-cdt 有问题,新的 C++ 项目 > hello world (cygwin gcc),右键单击 exe 文件,运行 as 并且控制台上没有显示任何内容。c项目也是一样。
I saw that my eclipse version was already a 32bits one.
我看到我的 eclipse 版本已经是 32 位的了。
I figured this out and here is (the) mysolution:
我想通了,这是我的解决方案:
There are several compilation profiles in eclipse cdt: release and debug. The default profile in eclipse cdt is debug. So, launching exe using "run as" does not work, you should launch it using "debug as". In my case, I was using a really recent cygwin installation with gcc installed, but with gdb not yet installed. I had to install gdb package in cygwin using cygwin-setup. I reran the exe using "debug as" and it worked.
eclipse cdt 中有几个编译配置文件:release 和 debug。eclipse cdt 中的默认配置文件是 debug。因此,使用“run as”启动 exe 不起作用,您应该使用“debug as”启动它。就我而言,我使用的是最近安装的 cygwin 安装了 gcc,但尚未安装 gdb。我必须使用 cygwin-setup 在 cygwin 中安装 gdb 包。我使用“调试为”重新运行了 exe 并且它起作用了。
I guess using release profile instead of default debug profile, rebuilding also works, and I guess that it's the same with mingw environment.
我想使用发布配置文件而不是默认的调试配置文件,重建也有效,我想这与 mingw 环境相同。

