在windows环境下通过虚拟机软件(Vmware)安装Ubuntu ,并设置网络参数保证系统能上网;熟练掌握Ubuntu常用命令,并学习使用vi/vim文本编辑软件和gcc编译软件。
文章目录
前言
Ubuntu系统一般采用 apt-get 命令进行某个软件的在线查找和安装,默认官方的软件源仓库位于国外,下载速度较慢。请修改系统参数,选择国内镜像软件源仓库,以提高软件安装速度。
一、c语言编写helloworld
1.在ubuntu系统中打开终端
2.在合适的位置建立文件夹helloworld
mkdir helloworld
3.打开文件夹helloworld
cd helloworld
4.用vim新建一个hello.c的c语言文件
vi hello.c
5.按i或a进入编辑模式,在光标处输入helloworld的c语言编程
#include <stdio.h>
int main()
{
printf("hello world!");
return 0;
}
6.点击Esc返回vim编辑器控制界面,点击英语的:(冒号),并在后面按下x或wq进行保存
7.用gcc命令编译hello.c
gcc hello.c -o hello
8.运行hello文件
。/hello
二、简单主/子程序的编写
(一)用ubuntu系统编写简单的主/子程序
1.编写一个main.c的主程序
vi main.c
2.编写代码后保存并退出
#include <stdio.h>
#include"sub.c"
int main()
{
int a=2,b=5;
float c;
float x2x(int a,int b);
c=x2x(a,b);
printf("%f",c);
return 0;
}
3.新建一个子程序sub.c并输入代码
vi sub.c
#include <stdio.h>
float x2x(int a,int b)
{
float c;
c=a+b;
return c;
}
4.编译并运行main文件
gcc main.c -o main
。/main
(二)在windows系统下编写简单主/子程序
1.使用编程软件Dev-C++
2.新建源文件main.cpp
#include <stdio.h>
#include"sub.h"
int main()
{
int a=2,b=5;
float c;
float x2x(int a,int b);
c=x2x(a,b);
printf("%f",c);
return 0;
}
3.新建头文件sub.h
#include <stdio.h>
float x2x(int a,int b)
{
float c;
c=a+b;
return c;
}
4.编译并运行
三、ubuntu系统下用Makefile放式编程
1.安装make
sudo apt-get install make
2.用vi编写一个主程序main.c和一个子程序sub.c,和上文(二)中相同
#include <stdio.h>
#include"sub.c"
int main()
{
int a=2,b=5;
float c;
float x2x(int a,int b);
c=x2x(a,b);
printf("%f",c);
return 0;
}
#include <stdio.h>
float x2x(int a,int b)
{
float c;
c=a+b;
return c;
}
3.用gcc命令把main.c文件和sub.c文件生成main.o和sub.o文件
gcc main.c -p main.o
gcc -c sub.c -o sub.o
4.创建并用getdit编译makeflie文件
touch makefile
vi makefile
main: main.o sub.o
gcc -o main main.o sub.o
main.o:main.c sub.h
gcc -c main.c
sub.o:sub.c sub.h
gcc -c sub.c
clean:
rm *.o main
~
5.用make命令运行makefile,并出结果
四、总结
安装好VM以及Ubuntu之后,通过百度以及CSDN上的已有博客掌握了vi/vim编辑器的基本用法,掌握了在Ubuntu系统中进行下载和更新的操作以及对makefile的基本操作。在学习过程中也遇到很多困难,例如:下载过程缓慢,不知道如何进行操作;因为我们软件下载之后资源包都是外国的导致网速很慢,将其改为中国的资源包之后网速会大大增加,另外如果有操作不熟的,可以在网上搜索网上会有详细的教程,我相信以后对虚拟机的操作一定会更加熟练。