對於每個新手來說,進入Ubuntu最想做的事莫過於在終端(Terminal)里運行自己的第一個C/C++程序"hello.c/hello.cpp"了。
很多語言書籍都是默認搭載好運行環境的,而Ubuntu默認是不包含編輯器vim。假設你和我一樣,展現在自己眼前的是一台剛安裝好的Ubuntu電腦,下面我們將來實現自己的第一個程序。
1.准備工作
1.1 打開控制台:使用快捷鍵 Ctrl + Alt + T;
1.2 安裝vim:輸入 sudo apt-get install vim ;
*1.3 安裝gcc(如果沒有):輸入 sudo apt-get install gcc 。
2.編寫hello.c源代碼
2.1 新建文件名為hello.c的源文件:輸入 vim hello.c ;
2.2 鍵入i 進入insert模式(即編輯模式),寫入如下經典代碼:
1 //the first program hello.c
2 #include<stdio.h>
3 int main(void) 4 { 5 printf("Hello, world!\n"); 6 return 0; 7 }
2.3 輸入完成后,Esc 回到normal模式(命令模式),鍵入 :wq 保存退出vim。
3.編譯hello.c
在終端執行 gcc hello.c -o hello 編譯。
4.運行程序hello
./hello 就看到結果:
Hello,world!