如何使用 Eclipse 和 CDT 使用 MinGW 编译和运行 C++?

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

How to compile and run C++ with MinGW using Eclipse and CDT?

c++eclipseconfigurationmingweclipse-cdt

提问by Jonas

I would like to do some C++ development on Windows using Eclipse and the CDTplugin. I use Eclipse Helios SR1 and have installed the CDT plugin. I have also installed MinGWand now I wrote a simple "Hello World" in Eclipse.

我想使用 Eclipse 和CDT插件在 Windows 上进行一些 C++ 开发。我使用 Eclipse Helios SR1 并安装了 CDT 插件。我还安装了MinGW,现在我在 Eclipse 中编写了一个简单的“Hello World”。

hello.cpp

你好.cpp

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello World" << endl;
    return 0;
}

In Eclipse using the CDT plugin and the MinGW compiler. How can I compile my program? And how can I test run the program from within Eclipse?

在 Eclipse 中使用 CDT 插件和 MinGW 编译器。如何编译我的程序?以及如何在 Eclipse 中测试运行程序?

回答by JFXNerd

Here is another good resource for installing MinGW on Eclipse: http://chrismwright.wordpress.com/2012/10/13/installing-eclipse-for-cc-and-mingw/

这是在 Eclipse 上安装 MinGW 的另一个很好的资源:http: //chrismwright.wordpress.com/2012/10/13/installing-eclipse-for-cc-and-mingw/

Pretty much a step-by-step installation including Hello World example at the end...

几乎是一步一步的安装,包括最后的 Hello World 示例......

回答by AndriiL

After browsing many threads and articles I've found a solution. Solution tested on Windows 10 x64 on Eclipse Neon.3 Release (4.6.3) with C/C++ Development Tools 9.2.1.201704050430 and MinGW

在浏览了许多主题和文章后,我找到了解决方案。解决方案在 Eclipse Neon.3 Release (4.6.3) 上的 Windows 10 x64 上测试,使用 C/C++ 开发工具 9.2.1.201704050430 和 MinGW

System configuration

系统配置

  1. Download MinGW. Any distro might work. I used the distrorecommended on http://isocpp.org/
  2. Extract archive into C:\MinGW(actually to C:\, because archive contains folder MinGW)
  3. RMB on This PC-> Properties-> Additional system settings-> Tab Advanced-> Button Environment variables
  4. On second table System variablesclick New. Name variable MINGW_HOMEand set path to MinGW install folder C:\MinGWand then OK
  5. Find variable Pathin table and choose Edit
  6. In new window click Newand type %MINGW_HOME%\bin\
  7. Confirm actions by clickig OKin opened windows
  8. You can check availability of new tools by typing in command line g++ --versionYou should see something like
  1. 下载 MinGW。任何发行版都可能有效。我使用了http://isocpp.org/ 上推荐的发行版
  2. 将存档解压缩到C:\MinGW(实际上是到C:\,因为存档包含文件夹MinGW
  3. RMB on This PC-> Properties-> Additional system settings-> Tab Advanced-> ButtonEnvironment variables
  4. 在第二张桌子上System variables点击New。命名变量MINGW_HOME并将路径设置为 MinGW 安装文件夹C:\MinGW,然后OK
  5. Path在表中查找变量并选择Edit
  6. 在新窗口中单击New并键入%MINGW_HOME%\bin\
  7. OK在打开的窗口中通过 clickig 确认操作
  8. 您可以通过在命令行中键入来检查新工具的可用性g++ --version您应该看到类似

g++ (GCC) 6.3.0 Copyright (C) 2016 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

g++ (GCC) 6.3.0 Copyright (C) 2016 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Eclipse CDT configuration

Eclipse CDT 配置

  1. Install Eclipse with CDTor just add CDTto existing Eclipse installation
  2. Go to the folder with installed MinGW (C:\MinGW\bin\). Make a copy of file gcc.exe(DO NOT RENAME original file!)
  3. Rename copied filed to mingw32-gcc.exe(You should have both files gcc.exeand mingw32-gcc.exein \MinGW\bin\)
  4. Open Eclipse and select C\C++perspective
  5. Go to Window-> Preferences-> C\C++-> Build-> Environment
  6. Click Addand type PATHas name and click on Variablesand select Path. Confirm with Ok.
  7. Select new variable PATHby clicking Selectand then Ok.
  8. Restart Eclipse
  1. 使用 CDT安装Eclipse或仅将CDT添加到现有的 Eclipse 安装中
  2. 转到安装了 MinGW 的文件夹 ( C:\MinGW\bin\)。制作文件的副本gcc.exe(不要重命名原始文件!)
  3. 重命名复制到mingw32-gcc.exe(您应该同时拥有文件gcc.exemingw32-gcc.exe\MinGW\bin\
  4. 打开 Eclipse 并选择C\C++透视图
  5. Window-> Preferences-> C\C++-> Build->Environment
  6. 单击Add并键入PATH名称,然后单击Variables并选择Path。用 确认Ok
  7. PATH单击Select,然后单击选择新变量Ok
  8. 重启 Eclipse

Now you should be able to compile Hello World program. Just select New-> C++ Project. Here you should see available MinGW as Toolchain

现在您应该可以编译 Hello World 程序了。只需选择New-> C++ Project。在这里你应该看到可用的 MinGW 作为工具链

回答by Abelardo

Just add MinGWto System Path(System configuration part in AndriiL's post) is enough. The variable named PATHwith ${PATH}as value will be added automatically in Window -> Preferences -> C\C++ -> Build -> Environmentand you can see MinGW GCCas a toolchain option in project wizard. If no PATH variable presented, Eclipse CDT may not load the recent System Path changes in the OS(Click Select...and choose Path variable, the value may not contain MinGW path), just Exit Eclipse and open it again.

只需添加MinGW到系统路径(AndriiL 帖子中的系统配置部分)就足够了。PATH${PATH}as 值命名的变量将被自动添加Window -> Preferences -> C\C++ -> Build -> Environment,您可以MinGW GCC在项目向导中将其视为工具链选项。如果没有出现 PATH 变量,Eclipse CDT 可能不会加载操作系统中最近的系统路径更改(单击Select...并选择Path variable,该值可能不包含 MinGW 路径),只需退出 Eclipse 并再次打开它。