在 Windows 上设置 Eclipse C++ 编译器而无需自动安装或更改系统路径

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

Set up Eclipse C++ compiler without auto-install or altering System Path on Windows

c++eclipsegcc

提问by Organiccat

I am trying to install a C++ compiler on Eclipse without altering the Path variables as I can't, the machine has limited rights. Eclipse obviously runs fine, it's the build that doesn't, it complains about.

我正在尝试在 Eclipse 上安装 C++ 编译器而不更改 Path 变量,因为我不能,机器的权限有限。Eclipse 显然运行良好,它的构建没有,它抱怨。

The first thing I noticed was a warning that said "Unresolved inclusion" for the libary file stdio.h

我注意到的第一件事是一个警告,上面写着库文件 stdio.h 的“未解决的包含”

I added the path variable inside Eclipse's "Windows > Preferences > C/C++ > Environment" with a new environment variable named "Path" with a path to my minGW/bin folder but to no avail. I also tried setting it to "Replace the native environment variable with specified one" but also no change.

我在 Eclipse 的“Windows > Preferences > C/C++ > Environment”中添加了路径变量,并添加了一个名为“Path”的新环境变量,其中包含指向我的 minGW/bin 文件夹的路径,但无济于事。我还尝试将其设置为“用指定的替换本机环境变量”,但也没有更改。

The build errors out saying:

构建错误说:

****  WARNING: The "Default" Configuration may not build  ****
****  because it uses the "MinGW GCC"  ****
****  tool-chain that is unsupported on this system.  ****

and then

进而

(Cannot run program "make": Launching failed)

And of course no more. It's a simple Hello World test, so the code shouldn't be an issue. I can see the includes under a folder in the "Includes" area that Eclipse generates (D:\MinGW\binutils\lib) but clicking on them in the Outline tab of Eclipse brings up the error "No include files were found that matched that name".

当然不会了。这是一个简单的 Hello World 测试,因此代码应该不成问题。我可以在 Eclipse 生成的“包含”区域中的文件夹下看到包含 (D:\MinGW\binutils\lib),但是在 Eclipse 的“大纲”选项卡中单击它们会出现错误“没有找到匹配的包含文件姓名”。

回答by Kevin

It looks like you're trying to build a simple hello world program using Eclipse/CDT and a development environment and using mingw as the compiler tool chain. I was able to get this working just now without modifying my system path environment variable. This is what I did:

看起来您正在尝试使用 Eclipse/CDT 和开发环境并使用 mingw 作为编译器工具链来构建一个简单的 hello world 程序。我刚刚能够在不修改系统路径环境变量的情况下使其正常工作。这就是我所做的:

  • I already had Eclipse 3.5 (Galileo) installed with CDT
  • Installed MinGW to C:\MinGW (I assume you already had this done). Make sure the mingw-make component is installed (it's not installed by default, I had to check the box to install this component).
  • Create a new empty makefile project, add main.c, write hello world code (as you say this isn't the problem so I'm skipping detail here), add a new file called "makefile" and fill it in.
  • 我已经安装了带有 CDT 的 Eclipse 3.5 (Galileo)
  • 将 MinGW 安装到 C:\MinGW (我假设您已经完成了此操作)。确保 mingw-make 组件已安装(默认情况下未安装,我必须选中该框才能安装此组件)。
  • 创建一个新的空 makefile 项目,添加 main.c,编写 hello world 代码(因为你说这不是问题,所以我在这里跳过细节),添加一个名为“makefile”的新文件并填写它。

Contents of my main.c file

我的 main.c 文件的内容

    #include   
    int main()  
    {  
        printf("Hello World!");  
        return 0;  
    }  

Contents of my makefile:

我的makefile的内容:

    all:  
        gcc -o HelloWorld.exe main.c 
  • Open the project properties; Under C/C++ Build uncheck the "use default build command" and change the build command to "mingw32-make".
  • Under "C/C++ Build/Environment" add a new PATH variable with C:\Mingw\bin in the path
  • Under "C/C++ General/Paths and Symbols" add C:\mingw\include as an include path.
  • 打开项目属性;在 C/C++ Build 下取消选中“使用默认构建命令”并将构建命令更改为“mingw32-make”。
  • 在“C/C++ Build/Environment”下添加一个新的 PATH 变量,在路径中包含 C:\Mingw\bin
  • 在“C/C++ General/Paths and Symbols”下添加 C:\mingw\include 作为包含路径。

After doing this, my project built successfully and produced a HelloWorld.exe exectuable in my project.

这样做后,我的项目成功构建并在我的项目中生成了一个 HelloWorld.exe 可执行文件。

Another option that doesn't require adding a PATH variable to the system or project properties, or adding the include path to the project properties is to simply gives full path info to the commands in the makefile. For small projects this is manageable. Here's an example makefile:

另一个不需要向系统或项目属性添加 PATH 变量或向项目属性添加包含路径的选项是简单地为 makefile 中的命令提供完整路径信息。对于小型项目,这是可以管理的。这是一个示例生成文件:

Contents of makefile:

生成文件的内容:

    all:  
        c:\mingw\bin\gcc -o HelloWorld.exe -I c:\mingw\include main.c 

Of course you'll also have to change the build command from simply "mingw32-make" to "C:\mingw\bin\mingw32-make" as well.

当然,您还必须将构建命令从简单的“mingw32-make”更改为“C:\mingw\bin\mingw32-make”。

Another downside of this approach is that the CDT code parser will not be able to locate include files so you'll have warning in the editor to that effect.

这种方法的另一个缺点是 CDT 代码解析器将无法定位包含文件,因此您将在编辑器中收到警告。