天天看點

2-PLAT-Windows下用Eclipse搭建C/C++開發環境

 http://metaphy.iteye.com/blog/560534

覺得有用,果斷轉載。

本文假定你已經熟悉Java,Eclipse的安裝,并能順利啟動和運作Eclipse.此外因為各軟體版本在不斷更新,有些地方可能不準确,以最新的、原文資料為準。

距上一次寫和調C++程式,已經5、6年了,光陰荏苒歲月無情,現在再重新拾起來,很多東西都要從頭來。Windows下C/C++的IDE有很多,我知道的就有MS Visual Studio,Borland C++等,但這些是要版權的。不要錢也有一些,但因為對Eclipse太熟了,是以就選下面要講的Eclipse + GNU toolchain(話說toolchain這個詞很形象).

1. 首先下載下傳Eclipse for C++, 最新版是基于Eclipse 3.5.1的,叫做galileo(伽利略),受不了這種奇怪的名字了,為什麼不叫布魯諾?上個版本3.4貌似叫做ganymede(木衛三)。下載下傳位址:http://eclipse.org/downloads/,選擇32bit for windows,檔案名叫 eclipse-cpp-galileo-SR1-win32.zip

2-PLAT-Windows下用Eclipse搭建C/C++開發環境

2. 解壓,直接運作。注意,至少JDK你已經安裝了(我用的是JDK1.6)。運作後一個灰藍色的welcome頁面出現,進入Tutorials。學東西先讀Tutorial是個好習慣。

3. 首先了解一下什麼是CDT,就是 C/C++ Development Toolkit,bulabula... 然後它說,這個東西沒包含C/C++的編譯器、調試器,你得自己弄。

4. 那就繼續看。Windows下,MinGW和Cygwin 是擷取GNU toolchain的2種主要方式(GNU toolchain,GNU下一系列的工具包,我的了解主要是gcc這一系列工具)。這兩者最大的差別是MinGW使用Windows C的運作庫,叫做mscvrt,而Cygwin使用了一組基于GPL的DLLs(GPL協定具有傳染性,使用GPL協定下的軟體後你自己開發的東西也要遵守GPL協定),是以MinGW避開了GPL協定。

5. MinGW和CDT能很好的整合。好吧,我們裝MinGW(MinGW是Minimal GNU for Windows的意思,這個下載下傳過程相當慢,我下了大半個小時)。目前版本是MinGW-5.1.6.exe,我一股腦來了個Full install。裝完後才發現這麼一句:Do not install the MinGW Make feature as the MSYS version of make from step 5 is a more complete implementation of make.(不要安裝MinGW的Make, 第5步的MSYS是個更好的實作方案)

6. 為了避免将來可能遇到的問題,卸了重裝。這裡是完整的安裝步驟:

1)下載下傳MinGW,位址 http://sourceforge.net/projects/mingw/files/

2)安裝MinGW base tool和g++編譯器(不要安裝Make);我把除了Make之外的都裝了,裡面居然還有個Ada的編譯器

2-PLAT-Windows下用Eclipse搭建C/C++開發環境

3)目前版本(它是指MinGW-5.1.3,不過我下的5.1.6同樣也沒有)沒有裝gdb debugger, 下載下傳它:http://downloads.sourceforge.net/mingw/gdb-6.6.tar.bz2

4)解壓gdb-6.6.tar.bz2 到你安裝MinGW的地方,gdb-6.6/下也有一系列bin,inclue檔案夾,直接拷到MinGW下面覆寫進去即可

5)如果要用Makefile,請下載下傳 MSYS-1.0.10.exe,MSYS是make及指令行的一個實作。嗯,要用。下載下傳位址 http://downloads.sourceforge.net/mingw/MSYS-1.0.10.exe

安裝界面是個指令界面,寫2個”y”,然後告知MinGW的安裝路徑即可。

2-PLAT-Windows下用Eclipse搭建C/C++開發環境

OK,安裝部分就完成了。下面寫2個小例子。

7. 首先建立一個簡單的HelloWorld C++工程,這個很簡單,按Wizard向導建一個模闆即可。

2-PLAT-Windows下用Eclipse搭建C/C++開發環境

Run的時候選Run Configurations, 然後輕按兩下C/C++ application建一個新的run configuration就行。

2-PLAT-Windows下用Eclipse搭建C/C++開發環境

8. 下面建一個Makefile類型的工程。選擇New C++ Project -> Makefile project -> Empty Project, 我們建一個空的項目,建完後裡面什麼也沒有(除了2個.project檔案),這時,我們要建一個源檔案和一個make檔案:main.cpp 和 makefile,如下,都建到根目錄下:

2-PLAT-Windows下用Eclipse搭建C/C++開發環境

C++代碼

2-PLAT-Windows下用Eclipse搭建C/C++開發環境
2-PLAT-Windows下用Eclipse搭建C/C++開發環境
2-PLAT-Windows下用Eclipse搭建C/C++開發環境
  1. #include <iostream>   
  2. using namespace std;   
  3. int main () {   
  4.     // Say Hello five times   
  5.     for (int index = 0; index < 5; ++index)   
  6.       cout << "HelloWorld!" << endl;   
  7.     char input = 'i';   
  8.     cout << "To exit, press 'm'" << endl;   
  9.     while(input != 'm') {   
  10.         cin  >> input;   
  11.         cout << "You just entered " << input   
  12.              << " you need to enter m to exit." << endl;   
  13.     }   
  14.     exit(0);   
  15. }  
/*
 * main.cpp
 */

#include <iostream>
using namespace std;

int main () {
    // Say Hello five times
    for (int index = 0; index < 5; ++index)
      cout << "HelloWorld!" << endl;
    char input = 'i';
    cout << "To exit, press 'm'" << endl;
    while(input != 'm') {
        cin  >> input;
        cout << "You just entered " << input
             << " you need to enter m to exit." << endl;
    }
    exit(0);
}      

Makefile代碼

2-PLAT-Windows下用Eclipse搭建C/C++開發環境
2-PLAT-Windows下用Eclipse搭建C/C++開發環境
2-PLAT-Windows下用Eclipse搭建C/C++開發環境
  1. all: hello.exe   
  2. clean:   
  3.     rm main.o hello.exe   
  4. hello.exe: main.o   
  5.     g++ -g -o hello main.o   
  6. main.o:   
  7.     g++ -c -g main.cpp  
all: hello.exe

clean:
	rm main.o hello.exe

hello.exe: main.o
	g++ -g -o hello main.o

main.o:
	g++ -c -g main.cpp      

注意,makefile裡的行首縮進用的是Tab而不是空格。如果編譯時提示 No separator...就是這裡有問題。

9. Ok, 選中工程,點Build(或點那個小錘子),你會發現這個錯誤:(Cannot run program "make": Launching failed),啊,我們的make.exe還沒設。選中工程,直接Alt-Enter到工程屬性頁,把msys的bin加到Path裡。

2-PLAT-Windows下用Eclipse搭建C/C++開發環境

10. 重新build, 大功告成。

繼續閱讀