经过长时间学习和研究linux GNU make工程管理器 ,现在把学习心得与大家分享一下,希望本文能教会您一些有用的东西。
make工具,是所有想在Linux/Unix系统上编程的用户都需要且必须掌握的工具。如果您写的程序没有用到make工具,则说明您写的程序仅仅是个人练习小程序,称不上有实用价值的程序,因此我们必须学习、掌握并灵活运用它。
在Linux/UNIX 系统中,习惯使用 Makefile或makfile 文件作为make命令目标文件。 Make工具最主要也是最基本的功能就是通过makefile文件来描述源程序之间的相互依赖关系并自动维护编译工作。而makefile 文件需要按照某种语法进行编写,文件中需要说明如何编译各个源文件并连接生成可执行文件,并要求定义源文件之间的依赖关系。
一、多文件编译的总体结构
如下图所示, 本示例 共包含 float类型加法、加法头函数、int类型加法、main主函数、float类型减法、减法头函数、int类型减法
主函数
#include "add.h"#include "sub.h"#include <stdio.h>int main(){ int x, y; float a, b; x=5; y=2; a=5.5; b=2.2; printf("%d + %d = %d/n", x, y, add_int(x, y)); printf("%3.1f + %3.1f = %3.1f/n", a, b, add_float(a, b)); printf("%d - %d = %d/n", x, y, sub_int(x, y)); printf("%3.1f - %3.1f = %3.1f/n", a, b, sub_float(a, b)); return 0;}
加法头函数
/* add.h */#ifndef _ADD_H_#define _ADD_H_extern int add_int(int x, int y);extern float add_float(float x, float y);#endif
int类型加法
/* add_int.c */int add_int(int x, int y){ return x+y;}
float类型加法
/* add_float.c */float add_float(float x, float y){ return x+y;}~
减法头函数
/* sub.h */#ifndef _SUB_H_#define _SUB_H_extern int sub_int(int x, int y);extern float sub_float(float x, float y);#endif
int类型减法
/* sub_int.c */int sub_int(int x, int y){ return x-y;}
float类型减法
/* sub_float.c */float sub_float(float x, float y){ return x-y;}