VMware配置Ubuntu 编写c程序
文章目錄
- Vmware配置Ubuntu
- Vim的使用
- Ubuntu配置
- apt-get配置
- 16.04
- 18.04
- 20.04
- 編寫(xiě)程序
- helloworld程序
- 編寫(xiě)簡(jiǎn)單程序
- (一)Ubuntu下的簡(jiǎn)單主/子程序
- (二)Windows下編寫(xiě)程序
- Ubuntu系統(tǒng)使用makefile方式編程
Vmware配置Ubuntu
參考該鏈接
Vim的使用
vim是一款編輯器,基于vi,可以通過(guò)按鍵就能完成文章的書(shū)寫(xiě),有三種使用模式。
| 命令模式 | 通過(guò)輸入vi的命令對(duì)文件的內(nèi)容進(jìn)行處理(復(fù)制、刪除、移動(dòng)等),也可以通過(guò)按光標(biāo)鍵來(lái)移動(dòng)光標(biāo) |
| 編輯模式 | 可以在光標(biāo)處輸入內(nèi)容 |
| 命令項(xiàng)模式 | 命令模式下,用戶(hù)輸入冒號(hào)后,光標(biāo)會(huì)跳到底行,然后輸入命令 |
常用命令
| h | 左移光標(biāo) |
| l | 右移光標(biāo) |
| k | 上移光標(biāo) |
| j | 下移光標(biāo) |
| i | 插入內(nèi)容 |
| l | 第一行插入 |
| a | 光標(biāo)后插入 |
| o | 當(dāng)前行插入新一行 |
| x | 刪除光標(biāo)處字符 |
| dd | 刪除所在行字符 |
| X | 刪除光標(biāo)前的一個(gè)字符 |
| :n1,n2 t n3 | 把n1行到n2行的內(nèi)容復(fù)制到第n3行的下一行 |
| :n1,n2 m n3 | 把n1行到n2行的內(nèi)容移動(dòng)到第n3行的下一行 |
| :w | 保存文件,不退出 |
| :q | 不保存,退出 |
| :wq/:x /:ZZ | 保存并退出 |
Ubuntu配置
apt-get配置
采用apt-get安裝某個(gè)軟件時(shí),默認(rèn)官方的軟件源倉(cāng)庫(kù)位于國(guó)外,下載速度慢,選擇國(guó)內(nèi)鏡像軟件源倉(cāng)庫(kù),提高安裝速度。
(1)首先切換目錄:cd /etc/apt,查看/編輯sources.list文件:sudo vi sources.list
可以看到都是國(guó)外的軟件源
(2)刪掉sources.list原本的內(nèi)容,選擇合適的源并配置,這里選擇阿里源,不同版本的Ubuntu對(duì)應(yīng)的內(nèi)容是不同的。
16.04
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial universe deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties deb http://archive.canonical.com/ubuntu xenial partner deb-src http://archive.canonical.com/ubuntu xenial partner deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse18.04
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse20.04
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse(3)文件內(nèi)容編輯完畢,保存退出,更新源
sudo apt-get update編寫(xiě)程序
helloworld程序
(1)打開(kāi)終端,選擇合適的位置,創(chuàng)建helloworld文件,通過(guò)vim進(jìn)行編輯文件。
(2)使用gcc命令編譯程序
gcc常用命令如下表
| gcc -E helloworld.c -o helloworld. i | 對(duì)helloworld.c 的程序進(jìn)行預(yù)編譯 .i做擴(kuò)展名,生成文本文件 |
| gcc -S helloworld.i -o helloworld.s | 進(jìn)行編譯,生成一個(gè)匯編語(yǔ)言源程序文件.s做擴(kuò)展名,編譯后是文本文件 |
| gcc -c helloworld.s -o helloworld.o | 對(duì)helloworld.s進(jìn)行匯編,生成一個(gè)可重定位目標(biāo)文件 . o作擴(kuò)展名,匯編后是二進(jìn)制文件 |
| gcc helloworld.o -o helloworld | 將多個(gè)可重定位目標(biāo)文件和標(biāo)準(zhǔn)庫(kù)函數(shù),printf所在的可重定位目標(biāo)模塊printf.o進(jìn)行鏈接,生成可執(zhí)行目標(biāo)文件 |
| gcc helloworld.c -o helloworld | 從c語(yǔ)言程序直接生成可執(zhí)行目標(biāo)文件 |
| ./helloworld | 執(zhí)行文件helloworld |
| (3)運(yùn)行helloworld文件 |
編寫(xiě)簡(jiǎn)單程序
(一)Ubuntu下的簡(jiǎn)單主/子程序
(1)編寫(xiě)一個(gè)main.主程序
vim main.c
編寫(xiě)子程序sub.c
(2)編譯main.c程序并運(yùn)行
(二)Windows下編寫(xiě)程序
這里使用VS2017新建一個(gè)空項(xiàng)目,編寫(xiě)main.cpp、sub.cpp、sub.h文件,點(diǎn)擊運(yùn)行,得到結(jié)果如圖。
Ubuntu系統(tǒng)使用makefile方式編程
一個(gè)工程文件中的源文件可能有很多,并且不同的功能、模塊等都放在不同的目錄中,常規(guī)的編譯已經(jīng)不能高效化的處理這樣的問(wèn)題,makefile寫(xiě)好后只需要一個(gè)make指令,即可完成makefile文件中所編寫(xiě)的所有指令,從而編譯整個(gè)工程文件。
main.c內(nèi)容
sub.h內(nèi)容
makefile內(nèi)容
運(yùn)行結(jié)果
總結(jié)
以上是生活随笔為你收集整理的VMware配置Ubuntu 编写c程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: minSdk(API 29) devi
- 下一篇: Ubuntu下使用gcc和makefil