在linux作業系統下寫c++程式
我們知道在windows下編譯並執行c++程式,只需要一個合適的集編輯,編譯,連線執行為一體的多功能軟體就可以完成整個過程。國內用的比較多的軟體有visual studio(版本現已更新到2017)初學者也可以考慮visual c++6.0;本章我們主要討論在如何linux系列作業系統中如何使用shell進行c++程式的編寫以及執行。
類比於寫一個執行在windows平臺的c++應用程式,在linux終端下首先也要建立一個字尾名為.cpp的檔案用來存放你的主要程式碼。
使用touch命令touch test.cpp 再ls一下會看到你的資料夾裡多了一個名為test.cpp的檔案。
vim test.cpp
進入檔案內編寫你的程式碼程式
#include<iostream>
2 using namespace std;
3 class Time{
4 private:
5 int hour;
6 int minute;
7 int second;
8 public:
9 void set(int h,int m,int s);
10 void print();
11 };
12 void Time::set(int h,int m,int s)
13 { hour=h;minute=m;second=s;}
14 void Time::print()
15 {cout<<hour<<"/"<<minute<<"/"<<second<<endl;}
16 int main()
17 { Time a,b;
18 a.set(10,12,50);
19 a.print();
20 b.set(12,10,40);
21 b.print();
22 return 0;
23 }
儲存退出
使用gcc編譯你所寫的程式gcc test.cpp 這個時候系統會預設生成一個名為a.out的檔案在你的當前目錄下。若想指定一個你所喜歡的檔名可以使用-o
gcc test.cpp -o 後面加你想生成的檔名。
如果編譯通過的話證明你所寫的程式是沒有bug的
執行一下 ./a.out
ok.