---
title: "Window+GCC+CDT用Eclipse開發C、C++- -（转）"
author: "simon_zhang"
date: "2006-07-22T13:14:00Z"
tags: []
slug: "window-gcc-cdt-eclipse-c-c"
draft: false
lastmod: "2006-07-22T13:14:00Z"
id: 64939
url: "https://mogee.iteye.com/blog/64939"
---

**原文地址：[http://ykaiyan.blogchina.com/blog/3372663.html](http://ykaiyan.blogchina.com/blog/3372663.html)**

**Tag**： [GCC](http://tag.bokee.com/tag/GCC)    [CDT](http://tag.bokee.com/tag/CDT)                                          

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/](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](http://www.mingw.org/download.shtml)

安裝：安裝目錄選C槽，然後狂點下一步(Next)就行了。安裝完後路徑是這  
樣-&gt;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，或在project properties-&gt;make project -&gt;  
將make 改為 mingw32-make ) -- 由 **snpshu** 補充。

在環境變數裡加入下列設定：  
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。

```
1
2
3
4
5
6
7
8
```

```
＃i nclude <iostream>
using namespace std;
 
int main(void) {
    cout << "Can You Feel My World？" ;
 
    return 0;
} 
```

在Command Line下編譯指令：

```
1
```

```
C:\g++ main.cpp -O3 -o hello
```

(O3的O是英文大寫"歐")  
編譯成功後：便會產生hello.exe的執行檔。  
執行畫面如下：

```
1
2
3
4
5
6
7
8
9
10
```

```
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-&gt;Open Perspective-&gt;C/C++ Development

◎步驟二.建立一個C++用的專案。  
File-New-&gt;Project-&gt;C++-&gt;Standard Make C++ Project  
(接下來的步驟跟建立一般的Java專案一樣，皆採預設即可)

◎步驟三.把我們剛剛寫的main.cpp import進來，加到專案裡。  
File-&gt;Import-&gt;File System-&gt;瀏覽C:\\main.cpp

◎步驟四.建立一個makefile。  
File-&gt;New-&gt;File，檔案名稱填：makefile。(不需打副檔名)

makefile內容如下：

```
1
2
```

```
all:
    g++  main.cpp -g -o run
```

注意：makefile縮排要以Tab鍵作縮排，不能以空格4作縮排，  
否則Build會有問題。

◎步驟五.設定Make Targets。  
Windows-Show View-&gt;Make Targets  
在Make Targets視窗裡按滑鼠右鍵，Add Build Target  
，name打：編譯。Build Target打：all。

◎步驟六.編譯。  
在剛剛建立的Make Targets "編譯" 上點滑鼠2下，即會開始編譯，  
此時我們可以發現hello.exe已經產生在我們專案下了。可在底下  
C-Build視窗看到以下輸出結果：

```
1
2
```

```
make -k all 
g++  main.cpp -g -o run
```

◎步驟七. \*.exe執行前設定。因為在Windows下Run，所以要先作個設定  
，請開啟Project-&gt;Properties-&gt;C/C++ Make Project-&gt;Binary Parser頁面。  
Binary Parser下拉式選單，將ELF Parser改成PE Windows Parser。

◎步驟八.執行。  
Run-&gt;Run as-&gt;C Local Application。  
在底下Consloe視窗看到hello.exe的執行結果。

註：當原始檔有修改，要重新編譯時，只要滑鼠雙擊我們在步驟五  
所建立的Make Targets "編譯"，即可Rebuilding。

附圖：  
[![](http://www.javaworld.com.tw/jute/upload/2004/06/12/25514566.gif)](http://www.javaworld.com.tw/jute/upload/2004/06/12/25514566.gif) 

 

**◎補充：**  
由於CDT的編輯器並無內建Formatter，如果要自動排版C/C++ Source  
Code 必須藉由其它排版工具來輔助。這裡我們要使用的是Artistic Style。  
它是個短小簡便而且免費的Formatter，除了可以排版C/C++ Source Code  
外，你也可以用來排版Java || C#。下載網址：  
[http://sourceforge.net/projects/astyle](http://sourceforge.net/projects/astyle)

在Eclipse我們必須藉由External Tools來啟動它。

Run-&gt;External Tools-&gt;External Tools，作以下設定：  
name：  
C\_C++ Formatter

location：  
C:\\Astyle\\astyle.exe (這裡設astyle.exe所在的完整路徑)

Working Directory：  
${workspace\_loc:/Hello} (請由Browse Button點選)

Arguments：  
\--style=kr ${resource\_name}

做好設定後，當focus在\*.cpp檔案上時，點選ToolBar上  
的外部工具啟動鈕，就可以排版了。

註：按下排版鈕後，Eclipse會顯示MessageBox提示檔案已變更  
，是否要載入，這時請選"Yes"。而Artistic Style有許多的細部  
參數可以設定排版效果，可自行參考裡面的說明文件。

設定圖解：  
[![](http://www.javaworld.com.tw/jute/upload/2004/06/12/59809590.gif "Click to view full AStyle.gif (657 X 564)")](http://www.javaworld.com.tw/jute/upload/2004/06/12/59809590.gif)