简体   繁体   English

在命令提示符下使用时,C ++“Hello World.exe”崩溃 - “Hello World.exe已停止工作。”

[英]C++ “Hello World.exe” crashes - “Hello World.exe has stopped working.” when used in command prompt

I've started learning C++ and wrote a "Hello World"-Program. 我开始学习C ++并写了一个“Hello World” - 程序。 When i try to run it in command prompt, it crashes and gives me a windows message saying "Hello World.exe has stopped working.". 当我尝试在命令提示符下运行它时,它崩溃并给我一条Windows消息,说“Hello World.exe已停止工作。”。

Code: 码:

#include <iostream>
#include <string>

int main()
{
    std::string response;

    std::cout << "Hallo Welt\n";
    std::cin >> response;
    std::cout << "User: " << response;

    return(0);
}

I'm using Eclipse IDE and MinGW as compiler. 我正在使用Eclipse IDE和MinGW作为编译器。 I've already set the path of MinGW/bin in the environment variables. 我已经在环境变量中设置了MinGW / bin的路径。 After i copied all dll's form the MinGW/bin directory into the folder of the Hello World.exe the program worked smoothly. 将所有dll从MinGW / bin目录复制到Hello World.exe的文件夹后,程序运行顺利。 So it seems that whatever it is that needs those dll's cannot access them properly. 所以似乎无论是什么需要那些dll都无法正确访问它们。 I'd like to spare me the hassle of copying the dll's for every project i create. 我想免除我为我创建的每个项目复制dll的麻烦。 Is there any way i could work around that? 有什么方法可以解决这个问题吗?

I'm not really confident about my computer and programming terminology yet so the description might seem a bit rough - help is still greatly appreciated! 我对我的计算机和编程术语并不十分自信,所以描述可能看起来有点粗糙 - 仍然非常感谢帮助!

Cheers! 干杯!

The following instructions did the work for me so I add them here for future reference as this problem took quite some time. 以下说明为我做了工作,所以我在这里添加它们以供将来参考,因为这个问题花了很长时间。

Prerequisites 先决条件

  • Windows 7 64-bit with all current Software Updates applied. 应用了所有当前软件更新的Windows 7 64位。
  • MinGW 0.5-beta–20120426–1 or later. MinGW 0.5-beta-20120426-1或更高版本。
  • Java JRE 1.7 or higher. Java JRE 1.7或更高版本。
  • Eclipse 日食

Installation Steps 安装步骤

  1. Install the Java JRE from Oracle. 从Oracle安装Java JRE。
  2. Install MinGW. 安装MinGW。 During the Select Components step, select “C++ Compiler” and “MSYS Basic System” for installation. 在“选择组件”步骤中,选择“C ++编译器”和“MSYS Basic系统”进行安装。
  3. Download Eclipse for C++ Developers. 下载Eclipse for C ++ Developers。
  4. Extract the Eclipse archive downloaded to your Downloads folder. 解压缩下载到Downloads文件夹的Eclipse存档。
  5. Open the Eclipse application found in the extracted Eclipse folder.[2] 打开提取的Eclipse文件夹中的Eclipse应用程序。[2]
  6. Test your Eclipse install by creating a new C++ Project from the File and New menu. 通过从“文件”和“新建”菜单创建新的C ++项目来测试Eclipse安装。
  7. Select “Hello World C++ Project” under Executable for the Project Type, and MinGW GCC for the Toolchain of your New C++ Project. 在项目类型的可执行文件下选择“Hello World C ++项目”,为新C ++项目的工具链选择MinGW GCC。 Type a Project Name and click the Finish button. 键入项目名称,然后单击“完成”按钮。
  8. Add “-static-libgcc -static-libstdc++” as Linker flags for your new project. 将“-static-libgcc -static-libstdc ++”添加为新项目的链接器标志。 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. 在Project Properties下,展开C / C ++ Build菜单,然后单击Settings。 Under the Tool Settings tab, expand the MinGW C++ Linker menu and click on Miscellaneous. 在“工具设置”选项卡下,展开“MinGW C ++链接器”菜单,然后单击“其他”。 Add the text to the Linker flags field, then click the Apply button. 将文本添加到“链接器标志”字段,然后单击“应用”按钮。
  9. Click Build Project under the Project menu, then click Run under the Run menu. 单击Project菜单下的Build Project,然后单击Run菜单下的Run。
  10. Confirm you have a functioning developer environment by viewing the output of the Console. 通过查看控制台的输出,确认您具有正常运行的开发人员环境。

Please note that step 8 is the one that solves the problem. 请注意, 第8步是解决问题的方法。

Note: You should also copy freeglut.dll in the directory where your .exe is located. 注意:您还应该在.exe所在的目录中复制freeglut.dll

I had the exact same problem. 我有同样的问题。 And what i found is that there might be different versions of the same DLL on my Path. 我发现我的Path上可能有相同DLL的不同版本。 So i simply moved my C:\\MinGW\\bin path from the end of the Path list to the front and now it's working. 所以我只是将我的C:\\ MinGW \\ bin路径从Path列表的末尾移动到前面,现在它正在工作。 But maybe this solution breaks some other software that relies on the other version of the DLL file. 但也许这个解决方案打破了依赖于其他版本DLL文件的其他软件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM