2008年9月24日星期三

[Ubuntu教學] 用Ubuntu 寫C++

最近因為學校課程需要開始在摸C++
Windows上的話我用的是Dev C++
可是在Ubuntu上我還沒找到很順手的IDE(Integrated Development Environment,集成開發環境)
用KDevelop狀況連連,編譯一直有問題;Eclipse 灌了可是也還沒摸熟;Anjuta的工具列好像少了什麼......囧

最後我找到一個方法
某個英文討論串上有個前輩也推荐先用這個方法
他說初學者應該先搞懂Linux 上編譯的技術,而不是一開始就讓IDE幫你處理的好好的。
就像讀數學應該先把觀念弄懂,而不是一開始就套速解公式。

==========以下正文開始==========

1.首先要先確定該有的套件都有了
我們需要安裝build-essential,以及編譯器g++
在終端機輸入以下這個指令安裝吧
sudo apt-get install g++ build-essential
2.接下來其實就可以開始寫程式了,當然你可以用Ubuntu內建的「文字編輯器」(gedit)
記得要選 顯示>>標示模式>>原始碼>>C++ 這樣他會自動幫你套色,看起來比較清楚



3.或者你可以用台灣人寫的文字編輯器MadEdit,很好用喔,會自動退格之類的很貼心
安裝請用指令
sudo apt-get install madedit

記得也要 檢視>>語法類型 選C++



4.記得用g++的時候請在程式的最後按Enter多留一行空白行,不然會編譯時會顯示錯誤訊息:「檔案未以空白列結束(no newline at end of file)」

5.程式寫好存成.cpp檔,接著就可以開始編譯了,例如我們把檔案存在
/home/a108210/cpp/
檔名叫
hello.cpp
那我們打開終端機,輸入
g++ /home/a108210/cpp/hello.cpp -o /home/a108210/cpp/hello.out -Wall
這個指令可以拆開來看
g++ /home/a108210/cpp/hello.cpp -o /home/a108210/cpp/hello.out -Wall
  • g++:這是編譯器compiler
  • /home/a108210/cpp/hello.cpp:這是cpp檔案所在位置
  • -o /home/a108210/cpp/hello.out:-o參數後面接的就是要輸出的檔案的位置+名稱,如你所見這邊是/home/a108210/cpp/hello.out
  • -Wall:加這個的目的是讓他把所有的警告訊息都顯示出來,免得他覺得不重要的訊息就自動隱藏了
接下來你就會看到檔案hello.out已經編譯好熱騰騰的躺在/home/a108210/cpp/裏面了

6.接下來我們切換到檔案所在的位置,在終端機輸入:
cd /home/a108210/cpp/
7.執行程式吧,輸入:
./hello.out
理論上你照著5.6.7.做的話會看到像這樣



那個hello world是因為示範用的程式碼是像這樣的:
#include
using namespace std;

int main()
{

cout << "hello world\n"; return 0; }

同場加映:如何用滑鼠取代cd指令
如果你有用lazybuntu懶人包,裏面有個很讚的東西,就是這個:



這有什麼用呢,請看:
1.例如我們剛剛操作的位置是/home/a108210/cpp/,那我們打開檔案管理員,直接開到這個位置按右鍵:



接著就會出現終端機



注意喔,終端機的位置直接就是/home/a108210/cpp/了。

2.既然位置都已經好了,那我們使用g++的時候就可以省去位置啦,因此我們簡化指令成為:
g++ hello.cpp -o hello.out -Wall
很清爽吧XD

3.而且如此一來上面的步驟6也可以直接跳過了,很棒吧!

如果你常常用終端機的話,這個東西可是妙用無窮呢XDD




14 則留言:

  1. 請愛用vim加上makefile
    如果覺得這樣很煩的話
    可以使用gedit的外部工具
    ex:
    echo 'Compiling...'
    if g++ $GEDIT_CURRENT_DOCUMENT_PATH -o $GEDIT_CURRENT_DOCUMENT_PATH.exe; then
    echo 'Running...'
    echo '-----------------------------------'
    $GEDIT_CURRENT_DOCUMENT_PATH.exe
    fi

    回覆刪除
  2. 喔喔喔 高手(拇指) 感謝啦

    回覆刪除
  3. = =,貌似#include后面还有东西吧...
    #include
    using namespace std;

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

    回覆刪除
  4. Outstanding story there. What occurred after?

    Thanks!

    My web page - laungage

    回覆刪除
  5. Hello to all, how is everything, I think every one is getting more from this website, and your views are nice in favor
    of new users.

    Also visit my page freak

    回覆刪除
  6. Very rapidly this web site will be famous among all blog viewers, due to it's good articles

    My web-site; zerde.snip.kz

    回覆刪除
  7. Hello there! I could have sworn I've been to this web site before but after looking at a few of the posts I realized it's new to me.

    Regardless, I'm definitely happy I discovered it and I'll be bookmarking it and checking back regularly!


    My blog post :: how to make a guy fall in love with you

    回覆刪除
  8. each time i used to read smaller posts which also clear their motive, and
    that is also happening with this article which I am reading at this
    place.

    Feel free to visit my website - airbnb blog

    回覆刪除
  9. For newest information you have to go to see world wide web and on the web I
    found this site as a most excellent site for most up-to-date updates.


    Also visit my blog post ... how to make a guy fall in love with you

    回覆刪除
  10. Whoa! This blog looks exactly like my old one! It's on a totally different topic but it has pretty much the same layout and design. Excellent choice of colors!

    Here is my weblog; couch surfing

    回覆刪除
  11. Spot on with this write-up, I absolutely believe that this website needs
    far more attention. I'll probably be back again to read more, thanks for the advice!

    Also visit my website: short term rentals

    回覆刪除
  12. I think this is one of the such a lot vital info for me.
    And i am happy studying your article. But want to statement on some basic issues, The website style is perfect, the articles is in reality excellent : D.
    Excellent activity, cheers

    Also visit my blog; airbnb coupons

    回覆刪除