如何使用 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
How to compile and run C++ with MinGW using Eclipse and 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 Jay
回答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
系统配置
- Download MinGW. Any distro might work. I used the distrorecommended on http://isocpp.org/
- Extract archive into
C:\MinGW
(actually toC:\
, because archive contains folderMinGW
) - RMB on
This PC
->Properties
->Additional system settings
-> TabAdvanced
-> ButtonEnvironment variables
- On second table
System variables
clickNew
. Name variableMINGW_HOME
and set path to MinGW install folderC:\MinGW
and thenOK
- Find variable
Path
in table and chooseEdit
- In new window click
New
and type%MINGW_HOME%\bin\
- Confirm actions by clickig
OK
in opened windows - You can check availability of new tools by typing in command line
g++ --version
You should see something like
- 下载 MinGW。任何发行版都可能有效。我使用了http://isocpp.org/ 上推荐的发行版
- 将存档解压缩到
C:\MinGW
(实际上是到C:\
,因为存档包含文件夹MinGW
) - RMB on
This PC
->Properties
->Additional system settings
-> TabAdvanced
-> ButtonEnvironment variables
- 在第二张桌子上
System variables
点击New
。命名变量MINGW_HOME
并将路径设置为 MinGW 安装文件夹C:\MinGW
,然后OK
Path
在表中查找变量并选择Edit
- 在新窗口中单击
New
并键入%MINGW_HOME%\bin\
OK
在打开的窗口中通过 clickig 确认操作- 您可以通过在命令行中键入来检查新工具的可用性
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 配置
- Install Eclipse with CDTor just add CDTto existing Eclipse installation
- Go to the folder with installed MinGW (
C:\MinGW\bin\
). Make a copy of filegcc.exe
(DO NOT RENAME original file!) - Rename copied filed to
mingw32-gcc.exe
(You should have both filesgcc.exe
andmingw32-gcc.exe
in\MinGW\bin\
) - Open Eclipse and select
C\C++
perspective - Go to
Window
->Preferences
->C\C++
->Build
->Environment
- Click
Add
and typePATH
as name and click onVariables
and selectPath
. Confirm withOk
. - Select new variable
PATH
by clickingSelect
and thenOk
. - Restart Eclipse
- 使用 CDT安装Eclipse或仅将CDT添加到现有的 Eclipse 安装中
- 转到安装了 MinGW 的文件夹 (
C:\MinGW\bin\
)。制作文件的副本gcc.exe
(不要重命名原始文件!) - 重命名复制到
mingw32-gcc.exe
(您应该同时拥有文件gcc.exe
和mingw32-gcc.exe
中\MinGW\bin\
) - 打开 Eclipse 并选择
C\C++
透视图 - 去
Window
->Preferences
->C\C++
->Build
->Environment
- 单击
Add
并键入PATH
名称,然后单击Variables
并选择Path
。用 确认Ok
。 PATH
单击Select
,然后单击选择新变量Ok
。- 重启 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 MinGW
to System Path(System configuration part in AndriiL's post) is enough. The variable named PATH
with ${PATH}
as value will be added automatically in Window -> Preferences -> C\C++ -> Build -> Environment
and you can see MinGW GCC
as 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 并再次打开它。