簡體   English   中英

使用cygwin的g ++ C ++編譯器

[英]Using the g++ C++ compiler from cygwin

我正在嘗試執行我的第一個“Hello World!” 在C ++中。 我使用的是Windows XP,我安裝了cygwin,其中安裝了g ++ C ++編譯器。 我寫了一個小的hello-world程序,並將其保存在hello.cpp中。 從命令提示符我寫:

g ++ hello.cpp

但我得到:

'g ++'不被識別為內部或外部命令,可操作程序或批處理文件。

我在我的D:\\ programs \\ cygwin中安裝了cygwin。 我在D:\\ cpp中使用我的hello-world文件創建了另一個目錄。 我的安裝或路徑的東西似乎不好,但我無法弄清楚是什么。 我運行了cygwin的exe文件,在安裝目錄中我有所有必需的文件,我認為:bin文件夾,lib,Cygwin.bat等。

我已經讀過,如果出現此類錯誤消息,我應該檢查cygwin1.dll文件是否已復制到Windows的主文件夾中。 這是C:\\ WINDOWS目錄嗎? 我看過那里,我那里沒有這樣的文件。 我在cygwin的bin文件夾中有cygwin1.dll:D:\\ programs \\ cygwin \\ bin。 另外,如何在命令的搜索路徑中檢查bin文件夾(D:\\ programs \\ cygwin \\ bin)?

我認為hello-world程序不應該包含任何語法錯誤,我剛剛復制了它。 還有,當我寫的時候

g ++ -v

我收到相同的錯誤消息:該命令無法識別。 如果有人給我一個暗示我應該看什么,我將不勝感激。 謝謝。

這是hello-world程序的代碼:

#include <iostream>
using namespace std;

int main() {
  cout <<"Hello World!"<<endl;
  return 0;
}

我已經安裝了g ++,現在當我輸入g ++ -v時,我得到:讀取規格:\\ d \\ cygnus \\ cygwin-b20 \\ H-i586-cygwin32 \\ bin .. \\ lib \\ gcc-lib \\ i586-cygwin32 \\ egcs-2.91.57 \\ specs gcc version egcs-2.91.57 19980901(egcs-1.1 release)

在bin目錄中,我有g ++。exe和gcc.exe。 我不明白為什么我得到“gcc”而不是“g ++”。 我不知道這有多重要,但是當我嘗試在DOS命令提示符下編譯程序時收到錯誤消息:

g ++ hello.cpp

我明白了:

hello.cpp:1: parse error before character 0357
hello.cpp: In function 'int main()':
hello.cpp:'cout' undeclared (first use this function)
hello.cpp: (Each undeclared identifier is reported only once
hello.cpp: for each function it appears in.)
hello.cpp: 'endl' undeclared (first use this function)

這是我嘗試在cygwin shell中編譯程序時得到的:

hello.cpp:1: parse error before character 0357
hello.cpp: In function 'int main()':
hello.cpp:'cout' undeclared (first use this function)
hello.cpp: (Each undeclared identifier is reported only once
hello.cpp: for each function it appears in.)
hello.cpp: 'endl' undeclared (first use this function)
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files

正如人們所說,檢查路徑。 不要開始復制cygwin DLL - 這是不必要的,可能會在以后發現問題並且無法解決問題。

當設置Windows PATH變量(和其他)時,我發現這個小程序非常有用,而且比眯着眼睛看MS控制面板aplet要好。

編輯:似乎OP錯誤地沒有安裝g ++ - 這是我們所有回答首先詢問明顯問題的人的一課:-)

啊,我以為我在安裝cygwin時安裝了g ++。 所以,我應該首先在那種情況下安裝g ++,不應該嗎?

(然后重啟)

我假設你在這里使用Windows Shell。 如果從cygwin Bash shell調用編譯器,則應為您設置所有路徑。

幾年前我寫了一篇關於這個的教程,這可能有所幫助:

http://www.codeguru.com/cpp/misc/misc/compilerandpre-compiler/print.php/c8107__1/

你安裝了g ++嗎? 它不是由Cygwin安裝程序的默認設置安裝的。

確保它在路徑上或您正在運行bash shell cmd提示符。

你確定你安裝了g ++嗎? G ++是C ++編譯器,而不是C編譯器。 如果您只安裝了C編譯器,那么您將擁有命令gcc但不能命令g ++

確保c:\\ cygwin \\ bin位於PATH中。

希望能幫助到你。

要獲得cygwin中的路徑:

echo $PATH

這將使我們走上理解問題的道路。

編輯

下一個命令將告訴我們cygwin認為它應該在windows中尋找所有這些文件

mount

包含/ usr / bin的行是我們需要的副本。

帶來dos shell。

cd c:\\ cygwin \\ bin

輸入c ++或cpp

或者做一個目錄命令看看內容。 尋找cpp或c ++。

如果它們不存在,則表示您尚未下載。

除了將其添加到路徑(這將解決您的問題)之外,您可能還想下載make和dbg,它們也在開發路徑上,但默認情況下不會檢查。 確保選擇“保持”作為整體安裝選項,以便它不會重新下載所有內容。

正如其他人所提到的,它可能是路徑問題,因此您需要查看環境變量PATH。 為此,請右鍵單擊“ My Computer ,然后單擊“屬性”。 轉到“高級”選項卡,然后單擊“環境變量”按鈕。 從這里,查看您提到的路徑是否包含在PATH變量中。 如果沒有,請使用您在那里看到的相同語法添加它們以分隔路徑(我認為它是路徑之間的分號或冒號。)

編輯:啊,我看到你已經完成了這件事(僅僅幾秒就擊敗了我的帖子。)我想你現在需要做的就是重啟。

嘗試創建一個新文件並自己鍵入代碼,而無需復制和粘貼它。 您的代碼中可能包含非法字符,這可能在您選擇的編輯器中不可見。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM