提示:本文目的是为了通过在Ubuntu系统上编译多个C程序来熟悉在Linux系统上编写C程序的操作。相关程序名称分别为main1.c、sub1.c、sub1.h;以下采用两种方式进行编译,第一种是直接使用gcc在命令行编译,第二种是使用Makefile文件编译。
一、直接使用gcc编译C程序
编写main1.c
#include"sub1.h"
int main()
{
int x=2,y=9;
printf("%.2f\n",x2x(x,y));
return 0;
}
编写sub1.h
#ifndef __SUB1_H
#define __SUB1_H
#include<stdio.h>
float x2x(int a,int b);
#endif
编写sub1.c
#include "sub1.h"
float x2x(int a,int b)
{
float ans;
ans=(float)b/a;
return ans;
}
直接用gcc编译运行
二、使用Windows
编码函数
运行结果
使用Makefile编程主程序
编写Makefile文件
main1:sub1.o main1.c
gcc main1.c sub1.o -o main1
sub1.o:sub1.c
gcc -c sub1.c -o sub1.o
clean:
rm *.o
总结
这次实验让我学会了在ubuntu上学会了编程,让我了解到了在windows上和linux上编程的不同之处,我还学会了使用make和makefile来维护多个文件。
参考文章:https://blog.youkuaiyun.com/qq_45237293/article/details/108741893