2016-09-14 45 views
4

我在Windows中有一個MinGW文件夾,並且我沒有在環境變量中設置任何路徑。當我運行以下命令:在Windows 7中運行MinGW gcc編譯器而不設置環境變量

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

我得到這個錯誤:

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

據我瞭解,這個問題所造成的,我沒有這個路徑添加到環境變量。如何在不向環境變量添加此路徑的情況下解決此問題,因爲我打算從Python腳本運行此命令。

+1

您只需調用'D:\ toolchains \ MinGW \ bin \ gcc.exe',而不是調用'gcc.exe'。只要您提供gcc的完整路徑,您甚至不需要在MinGW安裝目錄中。 – Tim

+0

@TimF當我這樣做時,我得到了同樣的錯誤:( –

+0

好吧,對不起,那麼它是gcc,沒有找到hw.c.所以無論你從hw.c文件夾執行這個命令,或者你給絕對或相對於hw.c的路徑例如'D:\ toolchains \ MinGW \ bin \ gcc.exe D:\ path \ to \ hw.c -o hw' – Tim

回答

3

您必須修改PATH環境變量或使用正確的工作目錄啓動gcc進程。你可以在Python一舉兩得:

我會建議修改PATH變量。

+0

我看不到任何理由,爲什麼工作目錄應該與此事有任何關係。使用MinGW g ++沒有設置搜索路徑或工作目錄對我來說工作得很好,而且一直如此(回到20世紀90年代)。但是,有時候連接器會出現問題,有時需要搜索'PATH',不僅需要它,而且還要在'PATH'中的其他搜索路徑之前。 –

1

您必須設置原始Mingw工作的PATH環境變量。見this,被稱爲「環境設置」一節:

  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.

否則,如果您使用了代碼塊的IDE,它會做所有這些骯髒的細節給你。有關如何更改Codeblock使用的默認Mingw編譯器的示例,請參見this。它有一個「自動檢測」功能,它將本地化​​Mingw編譯器,鏈接器等等。