Makefile使用
文章目录
1.要求
任务4、请编写一个主程序文件 main1.c 和一个子程序文件 sub1.c, 要求:子程序sub1.c 包含一个算术运算函数 float x2x(int a,int b),此函数功能为对两个输入整型参数做某个运算,将结果做浮点数返回;主程序main1.c,定义并赋值两整型变量,然后调用函数 x2x,将x2x的返回结果printf出来。1) 请在ubuntu系统用gcc 命令行方式编译主程序main1.c 并运行; 2) 请在windows系统下用你熟悉的编译工具编译主程序main1.c 并运行。
任务5、在任务一基础上,在ubuntu系统下用Makefile方式编程主程序。
任务4
1.1 main1.c (ubuntu)
#include <stdio.h>
#include "x2x.h"
int main(){
int a = 1, b = 2;
printf("%f\n", x2x(a,b));
return 0;
}
1.2 x2x.c (ubuntu)
#include "x2x.h"
float x2x(int a, int b){
return a*b;
}
1.3 x2x.h (ubuntu)
#ifndef _X2X_H
#define _X2X_H
#include <stdio.h>
float x2x(int a, int b);
#endif
1.3 gcc编译运行
1.4 windows下使用dev
任务5(使用Makefile)
2.1 Makefile是什么
通过makefile定义规则指定文件编译的顺序,实现“自动化编译”。使用make解释makefile中的指令。
2.2 Makefile的规则
目标文件 ... : 依赖文件 ...
(tab)make执行命令
...
...
2.3 编译程序
- makefile文件:
main11: main1.o x2x.o gcc main1.o x2x.o -o main11 main1.o: main1.c gcc -c main1.c x2x.o: x2x.c gcc -c x2x.c clean: rm *.o
- 执行make:
- 结果: