?Window+GCC+CDT用Eclipse開發C、C++
转载自:http://www.javaworld.com.tw/
Author:sungo
Eclipse除了可以開發Java之外,還支援了許多語言,現在先介紹 C、C++的開發環境設定,以後有機會再介紹其它的。Enjoy it!
OS:Windows XP Professional SP1 使用版本:Eclipse 2.1.2
一.首先要下載CDT,Eclipse 2.1.2使用者,請下載這項: CDT 1.2 Full for Windows R2.1.1 1.2.0 GA - Full - Windows。 Eclipse 2.1.3使用者請下載:CDT 1.2.1。 Eclipse 3.0 M7使用者請下載:CDT 2.0 M7。 Eclipse 3.0 M8使用者請下載:CDT 2.0 M8。 Eclipse 3.0 M9使用者請下載:CDT 2.0 M9。 下載網址:http://www.eclipse.org/cdt/
安裝:將解壓縮後的features、plugins整個資料夾複製到Eclipse安裝資料 裡,重新開啟Eclipse即可。
二.下載可在Windows上使用的GNU C、C++編譯器,這裡要下載的是:MinGW。 Download頁面很長的一串,請選擇這個版本: MinGW bin MinGW-3.1.0-1.exe 14863 kb Sep 15, 2003 11:14 下載網址:http://www.mingw.org/download.shtml
安裝:安裝目錄選C槽,然後狂點下一步(Next)就行了。安裝完後路徑是這 樣->C:\MinGW。
三.先在Command Line模式下測試編譯與執行。先將C:\MinGW\bin底下的 mingw32-make.exe更名為make.exe,因為待會在Eclipse使用時它預設 會抓系統裡make這個檔名而不是mingw32-make。
(註:如果不更名或是還有其他make程式時,也可以在稍後的Eclipse設定 中,在make targets view的地方,新增一個task時,build command 取消 use default , 使用 mingw32-make) -- 由 snpshu 補充。
及project properties->make project -> 將make 改為 mingw32-make ) 在環境變數裡加入下列設定: PATH : C:\MinGW\bin; (如果系統已經有裝其它C/C++編譯器,請把C:\MinGW\bin加在最前面。) LIBRARY_PATH :C:\MinGW\lib C_INCLUDE_PATH :C:\MinGW\include CPLUS_INCLUDE_PATH :C:\MinGW\include\c++\3.2.3;C:\MinGW\include\c++\3.2.3\mingw32; C:\MinGW\include\c++\3.2.3\backward;C:\MinGW\include
先使用文字編輯器編寫測試用的原始檔,檔名:main.cpp。
#include using namespace std;
int main(void) { cout << "Can You Feel My World?" ;
return 0; } 在Command Line下編譯指令:C:\g++ main.cpp -O3 -o hello (O3的O是英文大寫"歐") 編譯成功後:便會產生hello.exe的執行檔。 執行畫面如下:Microsoft Windows XP [版本 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Sungo>cd\
C:\>g++ main.cpp -O3 -o hello
C:\>hello Can You Feel My World? C:\> 註:-O3 旗標表示採最高級編譯最佳化,編譯速度最慢,但產生的執行檔 檔案會最小,執行速度會最快;-o 旗標表示將編譯完的*.exe重新更名。
◎步驟一.開啟Eclipse後,首先先開啟C/C++專用視景。 Windows->Open Perspective->C/C++ Development
◎步驟二.建立一個C++用的專案。 File-New->Project->C++->Standard Make C++ Project (接下來的步驟跟建立一般的Java專案一樣,皆採預設即可)
◎步驟三.把我們剛剛寫的main.cpp import進來,加到專案裡。 File->Import->File System->瀏覽C:\main.cpp
◎步驟四.建立一個makefile。 File->New->File,檔案名稱填:makefile。(不需打副檔名)
makefile內容如下:all: g++ main.cpp -g -o run 注意:makefile縮排要以Tab鍵作縮排,不能以空格4作縮排, 否則Build會有問題。
◎步驟五.設定Make Targets。 Windows-Show View->Make Targets 在Make Targets視窗裡按滑鼠右鍵,Add Build Target ,name打:編譯。Build Target打:all。
◎步驟六.編譯。 在剛剛建立的Make Targets "編譯" 上點滑鼠2下,即會開始編譯, 此時我們可以發現hello.exe已經產生在我們專案下了。可在底下 C-Build視窗看到以下輸出結果:make -k all g++ main.cpp -g -o run
◎步驟七. *.exe執行前設定。因為在Windows下Run,所以要先作個設定 ,請開啟Project->Properties->C/C++ Make Project->Binary Parser頁面。 Binary Parser下拉式選單,將ELF Parser改成PE Windows Parser。
◎步驟八.執行。 Run->Run as->C Local Application。 在底下Consloe視窗看到hello.exe的執行結果。
註:當原始檔有修改,要重新編譯時,只要滑鼠雙擊我們在步驟五 所建立的Make Targets "編譯",即可Rebuilding。
附圖:
sungo edited on 2004-06-17 20:36 
|