歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux系統如何寫C/C++程序

Linux系統如何寫C/C++程序

日期:2017/3/3 12:18:32   编辑:Linux技術

前言,本文介紹的利用linux系統編寫C/C++語言程序是在CentOS 環境下實現的,在編程之前,您得先安裝Linux系統或者Linux虛擬機,一切准備好之後我們就可以開始編程了。(1).開啟電腦或Linux虛擬機(本人使用的是虛擬機)

650) this.width=650;" width="553" height="262" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

(2)創建文件

點擊左上角應用程序->系統工具->終端

650) this.width=650;" width="553" height="205" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

得到如下結果

650) this.width=650;" width="554" height="303" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

(3)編輯文件

在輸入框中輸入vim hello.c //這裡vim指創建一個文件。hello.chello是文件名可隨意取,

//.c是文件類型,這裡指創建一個名稱為hello的c文件

650) this.width=650;" width="554" height="171" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

按下回車鍵得到

650) this.width=650;" width="554" height="366" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

按下字符鍵’i’

650) this.width=650;" width="554" height="384" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

開始輸入一段C語言文件(這裡我們先輸入一段錯誤代碼,看看怎麼調試修改),例:

650) this.width=650;" width="554" height="386" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

在上邊的程序裡,我再printf後邊沒有加分號。

現在我們保存這段程序,保存方法:1.按下“Esc”鍵。2.輸入“:wq!”。3.按下回車。

注意:雙引號裡邊的字符一個都不能少!

過程及結果如下:

第2 步完成後:

650) this.width=650;" width="554" height="386" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

按下回車之後:

650) this.width=650;" width="554" height="168" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

(4)調試及運行

首先進行編譯,輸入gcc hello.c -o hello 並按下回車

//作用是將c文件,hello.c編譯生成hello.obj文件

//如果編寫的是c++文件,則輸入g++ hello.cpp -o hello

按下回車之前

650) this.width=650;" width="554" height="153" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

按下回車之後

650) this.width=650;" width="554" height="144" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

看上邊給出了錯誤提示,我們來修改錯誤,

輸入vim hello.c

650) this.width=650;" width="554" height="149" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

按下回車

650) this.width=650;" width="554" height="376" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

現在看到這個界面還不能進行修改,需要在鍵盤上按下‘A’,結果如下,看看和上面有什麼不同

650) this.width=650;" width="554" height="388" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

修改錯誤(加上我們的分號)

並按下ESC 以及輸入 :wq!

650) this.width=650;" width="554" height="387" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

按下回車

650) this.width=650;" width="554" height="192" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

現在輸入gcc hello.c -o hello

650) this.width=650;" width="554" height="173" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

按下回車沒有出現錯誤即編譯成功

650) this.width=650;" width="554" height="169" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

輸入./hello並按下回車即可運行編寫的程序

650) this.width=650;" width="554" height="192" /e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" src="/e/u261/themes/default/images/spacer.gif" alt="spacer.gif" />

之所以上邊“hello word !”和後邊的語句連在一起,是因為我沒有加分號。

如果要寫C++程序,只需將gcc換為g++以及.c換成.cpp。

具體過程就在上邊,本人也是初學者,如果哪位大神發現有錯誤的地方,歡迎您批評指正!

本文出自 “分享中進步” 博客,請務必保留此出處http://xmwen1.blog.51cto.com/10730069/1706391

Copyright © Linux教程網 All Rights Reserved