运行 MinGW gcc 编译器 Windows 7 没有设置环境变量

Run MinGW gcc compiler in Windows 7 without setting environment variables

我在 Windows 中有一个 MinGW 文件夹,我没有在环境变量中设置任何路径。当我运行以下命令时:

D:\toolchains\MinGW\bin>gcc.exe hw.c -o hw

我收到这个错误:

gcc.exe: error: CreateProcess: No such file or directory

据我了解,这个问题是我没有把这个路径添加到环境变量中引起的。如何在不将此路径添加到环境变量的情况下解决此问题,因为我计划从 Python 脚本 运行 此命令。

您必须设置 PATH 环境变量,原始 Mingw 才能工作。请参阅 this,名为 "Environment Settings" 的部分:

  1. Right-click on your "My Computer" icon and select "Properties".
  2. Click on the "Advanced" tab, then on the "Environment Variables" button.
  3. You should be presented with a dialog box with two text boxes. The top box shows your user settings. The PATH entry in this box is the one you want to modify. Note that the bottom text box allows you to change the system PATH variable. You should not alter the system path variable in any manner, or you will cause all sorts of problems for you and your computer!
  4. Click on the PATH entry in the TOP box, then click on the "Edit" button
  5. Scroll to the end of the string and at the end add

    ;<installation-directory>\bin

  6. press OK -> OK -> OK and you are done.

否则,如果您使用像 Codeblocks 这样的 IDE,它会为您完成所有这些肮脏的细节。有关如何更改 Codeblocks 使用的默认 Mingw 编译器的示例,请参阅 。它有一个 "auto detect" 功能,可以本地化 Mingw 编译器、链接器等。

您要么必须修改 PATH 环境变量,要么使用正确的工作目录启动 gcc 进程。您可以在 python:

中同时执行这两项操作
  • Modify environment variables from within python

  • Specify a working directory for a subprocess in python

我建议修改 PATH 变量。