一步一步写Makefile(1):概念,变量,自动推导,Makefile相互引用
2017-08-23 15:43
309 查看
一步一步写Makefile
概念:
Make命令执行时是一步一步去解析Makefile文件内容
1. 每一个源文件都对应一个.o中间文件
2. 链接时就是链接这些中间文件里的全局变量和函数,如extern变量,包含了别的源文件里的函数声明并且用了这个函数,链接时就要去找这个函数的实现了
3. 中间文件.o太多,于是就有了库.a文件了,他是多个.o文件的打包,这样就不用指明很多个中间链接文件名**.o了,只需要指明一个.a文件就可以。
4.
cc编译用法:
[root@ouyanghaibin lesson6]#cc --help
用法:cc [选项] 文件...
例如[root@ouyanghaibinlesson6]# cc -c -o main.o main.c
[root@ouyanghaibinlesson6]# cc -c -o main.o main.c
[root@ouyanghaibinlesson6]# cc -c main.c
[root@ouyanghaibinlesson6]# cc main.c -c -o main.o
[root@ouyanghaibinlesson6]# cc main.c -o main 生成可执行文件
从这些用法例子可以看出,选项和源文件前后顺序可以随便放,但是要满足一个完整选项必须整个在一起,如-c -omain.o main.c 三个选项各个项必须完整
cc不但可以生成中间文件也可以生成可执行文件
常用选项:
-E 仅作预处理,不进行编译、汇编和链接
-S 编译到汇编语言,不进行汇编和链接
-c 编译、汇编到目标代码,不进行链接
-g 加入调试信息
-o <文件> 输出到 <文件>
概念:
Make命令执行时是一步一步去解析Makefile文件内容
1. 每一个源文件都对应一个.o中间文件
2. 链接时就是链接这些中间文件里的全局变量和函数,如extern变量,包含了别的源文件里的函数声明并且用了这个函数,链接时就要去找这个函数的实现了
3. 中间文件.o太多,于是就有了库.a文件了,他是多个.o文件的打包,这样就不用指明很多个中间链接文件名**.o了,只需要指明一个.a文件就可以。
4.
cc编译用法:
[root@ouyanghaibin lesson6]#cc --help
用法:cc [选项] 文件...
例如[root@ouyanghaibinlesson6]# cc -c -o main.o main.c
[root@ouyanghaibinlesson6]# cc -c -o main.o main.c
[root@ouyanghaibinlesson6]# cc -c main.c
[root@ouyanghaibinlesson6]# cc main.c -c -o main.o
[root@ouyanghaibinlesson6]# cc main.c -o main 生成可执行文件
从这些用法例子可以看出,选项和源文件前后顺序可以随便放,但是要满足一个完整选项必须整个在一起,如-c -omain.o main.c 三个选项各个项必须完整
cc不但可以生成中间文件也可以生成可执行文件
常用选项:
-E 仅作预处理,不进行编译、汇编和链接
-S 编译到汇编语言,不进行汇编和链接
-c 编译、汇编到目标代码,不进行链接
-g 加入调试信息
-o <文件> 输出到 <文件>
main.c #include<stdio.h> #include "test1.h" #include "test1.h" //多次包含也不会出错 int test2(void); int main(void) { printf("main.c\n"); test1(); test2(); } test1.c #include<stdio.h> int test1(void) { printf("test1.c\n"); } test1.h #ifndef _TEST1_ #define _TEST1_ int test1(void); #endif test2.c #include<stdio.h> int test2(void) { printf("test2.c\n"); } Makefile1 include Makefile2 Makefile2 variable=main.o test1.o test2.o main:$(variable) cc -o main $(variable) #进一步加入自动推导之后 $(varible): :test1.h clean: -rm $(variable) main #正确 ##################1:最原始的Makefile################### main:main.o test1.o test2.o cc -o main main.o test1.o test2.o main.o:main.c test1.h test2.h cc -c main.c test1.h test1.o:test1.c cc -c test1.c test2.o:test2.c cc -c test2.c clean: rm main.o test1.o test2.o main #################2:加入变量后的Makefile################# variable=main.o test1.o test2.o #variable:main.o test1.o test2.o 错误!产生不了main可执行文件 #所以严格用法:符号用于Makefile的一条规则里目标和依赖之间 #=符号用于变量定义 #也可以用:=定义变量,可避免变量循环递归定义variable:=main.o test1.o test2.o #main:variable #错误!必须要有$符号 main:$(variable) cc -o main $(variable) main.o:main.c test1.h cc -c main.c test1.h test1.o:test1.c cc -c test1.c test2.o:test2.c cc -c test2.c clean: rm main.o test1.o test2.o main #rm $(variable) main 正确 #################3:加入自动推导后的Makefile################# variable=main.o test1.o test2.o main:$(variable) cc -o main $(variable) main.o:test1.h #正确,可以自动推导出来main.c 当然像test1.h这样的头文件是推导不出来的 #只能推导出和目标同名的源文件 main.o:test1.h # cc test1.h 错误!cc里源文件要写就得写完整,要不就一个不写 test1.o: test2.o:test2.c clean: rm $(variable) main #正确 #################4:进一步加入自动推导之后的Makefile################# variable=main.o test1.o test2.o main:$(variable) cc -o main $(variable) #进一步加入自动推导之后 main.o test1.o test2.o:test1.h #$(varible):test1.h 也正确 clean: rm $(variable) main #正确 #################5:引用别的Makefile之后的Makefile################# include Makefile2 #将会把Makefile2中的内容在这里展开,执行Makefile2中的规则,产生 #Makefile2的目标,注意#include后面不要<>尖括号
相关文章推荐
- makefile提高篇—自动推导和自动化变量
- 自动变量的makefile
- makefile 自动变量
- Makefile 中常见自动变量和命令行选项
- Linux之Makefile(make自动推导)
- 【C/C++学院】0814-引用高级、引用高级增加/auto自动变量自动根据类型创建数据/Bool/Enum/newdelete全局/大数据乘法与结构体/函数模板与auto/宽字符本地化/inline
- OC学习篇之---数组对象的引用计数问题和自动释放池的概念
- [C#]一步一步开发自己的自动代码生成工具之二:SQLServer字段与C#变量的对应转化
- makefile 自动推导命令
- Makefile学习之让make自动推导
- Makefile 中常见自动变量和命令行选项
- JavaScript中变量的相互引用
- 一个通用的makefile写法,自动推导文件的依赖关系
- 黑马程序员------C语言中变量的概念,初始化和引用以及作用域
- makefile中的自动变量:$@ $^ $? $<
- 在makefile中递归引用变量
- makefile 自动推导命令
- makefile中的自动变量:$@ $^ $? $<
- Makefile 中常见自动变量和命令行选项
- 一个能自动搜索源文件并自动推导的Makefile