一 安裝
安裝步驟:
1、下載 Cygnus 的G++編譯器(http://www.claremontmckenna.edu/math/ALee/g++/full.exe),并保存到E盤(或其它任意盤),格式為E:full.exe。
2、直接運行該文件,可能需要十幾分鐘的時間(中間會有停頓甚至沒有任何提示性圖片或者符號,需要耐心等待哦)。
或者Win + R ,輸入cmd,按Enter鍵進入命令行模式;輸入E:,按Enter轉入E盤;輸入文件名稱full運行文件。
3、定位G++安裝后文件夾bin的位置,這里為E:/cygnus/cygwin-b20/H-i586-cygwin32/bin。設置環境變量如下:我的電腦->屬性->高級->環境變量,可在Administrator的用戶變量或系統環境變量中找到Path(大小寫均可),點擊編輯,在其后面加上“;E:/cygnus/cygwin-b20/H-i586-cygwin32/bin”,必須加上分號,且為英文半角符號。 最后點擊確定退出。
4、開始菜單中如果出現Cygnus Solutions 表示安裝成功了。
參考文獻:
1、http://www.claremontmckenna.edu/math/ALee/g++/g++.html
2、http://www.shnenglu.com/heidaizx/articles/33582.html
二 使用
1、 單一文件編譯
(1)使用UltraDdit創建并編輯hello.cpp(文件位置為E:/source/hello.cpp),內容如下:
- #include <iostream>
- using namespace std;
- int main()
- {
- cout<<"Hello,World!/n"<<endl;
- cin->get();
- return 0;
- }
(2)按Win + R ,輸入cmd,按Enter鍵進入命令行模式;
輸入E:,按Enter轉入E盤;
輸入cd source,轉入source文件夾;
輸入g++ Hello.cpp -o hello ,編譯并鏈接;
輸入hello運行文件,此時在屏幕上會顯示“Hello World !”。
2、多個文件編譯(1)使用UltraDdit創建并編輯Hello.h, Hello.cpp, MyFirst.cpp ,內容如下:
- //Hello.h, define Hello class
- class Hello
- {
- public:
- Hello();
- void Display();
- };
- //Hello.cpp
- #include <iostream>
- #include "hello.h"
-
- Hello::Hello()
- {
- }
-
- void Hello::Display()
- {
- cout<<"Hello, World!/n"<<endl;
- }
- //MyFirst.cpp, the main entry
- #include <iostream>
- #include "Hello.h"
- int main()
- {
- Hello theHello;
- theHello.Display();
- return 0;
- }
(2) 在g++中有一個參數-c 可以只編譯不連接,那么我們就可以按如下順序編譯文件 按照相同的步驟轉入source文件夾;
依次鍵入
g++ -c hello.cpp -o hello.o
g++ -c myfirst.cpp -o myfirst.o
g++ myfirst.o hello.o -o myfirst
然后輸入myfirst運行程序。截圖如下:

posted on 2014-06-24 19:43
聶文龍 閱讀(489)
評論(0) 編輯 收藏 引用