使用Windows和MinGW在Eclipse中编写的C++程序无法显示输出到控制台视图

Gqq*_*big 14 c++ eclipse console eclipse-cdt

我正在使用Windows 7 64位.

我安装了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;
}
Run Code Online (Sandbox Code Playgroud)

如果我运行此控制台程序,它应显示Hello world在Eclipse中的Console View,但不显示任何内容.

如果我转到调试文件夹并运行exe,它会打印到控制台.

如果我犯了一些语法错误,那么Eclipse Console View将显示一些内容,例如:

**** 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.   
Run Code Online (Sandbox Code Playgroud)

为什么Eclipse控制台视图中没有显示任何内容以及如何使我的C++控制台程序显示输出?

Vik*_*oss 22

我从这个网站找到了一个解决方法:http://www.eclipse.org/forums/index.php?= 42e862594001fa4469bbc834885d545f&t = msg&th = 197552

在该链接中,查看"无实名"的回复.

如果链接断开,这里是内容:

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.
Run Code Online (Sandbox Code Playgroud)


Ale*_*bny 5

问题是你的程序使用了 MinGW 的 dll 库 - 尝试手动启动 exe 文件,它会报告一些有关缺少 dll 的错误。

解决方案可以是,将所需的 dll 复制到项目目录中的 .exe 文件(以及 Release 或 Debug 子目录,取决于您使用 Run 命令执行的 .exe)。

或者,在菜单“运行”->“运行配置”中,选择用于该 .exe 文件的配置(或创建新的 C/C++ 应用程序配置),然后在右侧面板中选择“环境”选项卡。添加名为PATH的新变量,其值为c:\MinGW\bin(这是 mingw\bin 目录的默认路径,如果它在其他地方,请使用对您的安装有效的路径)
编辑:现在我正在查看 Vikyboss 的帖子,它是事实上相同 - 在运行配置中设置 PATH 变量。按照 Sydraps 的描述在“首选项”>“C/C++(展开)”>“环境”中设置 PATH 变量对我来说不起作用。

但我认为静态链接该库可能是最适合您的解决方案。在菜单Project -> Properties中选择C/C++ Build -> Settings。在右侧面板中选择您要更改的配置(您可以选择全部)。在“工具设置”选项卡中,选择MinGW C++ Linker -> Miscellaneous,然后在右侧面板的“链接器标志”中输入-static。现在,.exe 将因库的大小而变得臃肿(在我的例子中,Hello world 示例约为 +900kB,需要 2 个 dll),但它将独立于任何库。

我希望这对尝试开始使用 Eclipse C/C++ 并想知道为什么控制台中没有 Hello world 的人有所帮助。艾尔·楚布尼


Gqq*_*big 2

我找到原因了,只是因为我使用的是64位eclipse!

我转向 32 位 eclipse,相同的代码工作正常。