歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Ubuntu下編譯第一個C程序的成功運行

Ubuntu下編譯第一個C程序的成功運行

日期:2017/3/1 9:44:21   编辑:Linux編程

對於每個新手來說,進入Ubuntu最想做的事莫過於在終端(Terminal)裡運行自己的第一個C/C++程序"hello.c/hello.cpp"了。

很多語言書籍都是默認搭載好運行環境的,而Ubuntu默認是不包含編輯器vim和編譯器gcc。假設你和我一樣,展現在自己眼前的是一台剛安裝好的Ubuntu電腦,下面我們將來實現自己的第一個程序。

1.准備工作

1.1 打開控制台:使用快捷鍵 Ctrl + Alt + T;

1.2 安裝vim:輸入 sudo apt-get install vim;

1.3 安裝gcc:輸入 sudo apt-get install g++。

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

在終端執行 g++ hello.c -o hello 編譯。

4.運行程序hello

./hello 就看到結果:

Hello,world!

Ubuntu下編譯C程序的詳解 http://www.linuxidc.com/Linux/2012-11/73360.htm

Ubuntu下編譯和執行C程序 http://www.linuxidc.com/Linux/2009-10/22312.htm

Linux下用GCC編寫第一個C程序 http://www.linuxidc.com/Linux/2011-07/38415.htm

更多Ubuntu相關信息見Ubuntu 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=2

Copyright © Linux教程網 All Rights Reserved