大学有阵子做嵌入式的时候觉得Makefile简直高不可攀~~
教程大多数都在试图把许多细节都讲清楚~~没有跟教程死磕3天的劲头真的不容易搞懂Makefile
而且写Makefile需要一点点gcc编译器的储备知识。对于在linux上刚上手的开发者的确有点绕。
所以我在这里做了个最简版的Makefile,看懂这个,你大体能知道Makefile该怎么写~~~~其他细节,只能“大行不顾细谨”了
从这里下载Makefile最简版本:
http://download.youkuaiyun.com/detail/sera_ph/7111665
里面的文件为 DandL.txt Makefile outlib.c outlib.h testmake.c
其中main函数位于testmake.c中
testmake.c 中调用了由outlib.h声明outlib.c定义的一个函数。
使用gcc将这个最简单的工程编译链接的shell命令,在DandL.txt中
gcc -c outlib.c
gcc -c testmake.c
gcc testmake.o outlib.o -o testmake
gcc 目标文件 -o testmake的意思是将目标文件链接,生成可执行文件。testmake是可执行文件的名字。
这些就是你需要知道的gcc储备知识
Makefile就是针对这3个代码文件的最简版本
Makefile的基本语法如下:
target...(目标) : prerequisites...(依赖文件)
command(shell命令)
all: testmake.o outlib.o
gcc testmake.o outlib.o -o exeFile
outlib: outlib.c outlib.h
gcc -c outlib.c
testmake: testmake.c
gcc -c testmake.c
clean:
rm -f *.o exeFile
很明显,我们能看到,这个Makefile包含4个基本语句。
当在当前文件夹下,输入make outlib时,就相当于执行target为outlib的那语句的command
执行结果如下:
[***@localhost testmake]$ ls
DandL.txt Makefile outlib.c outlib.h testmake.c
[***@localhost testmake]$ make outlib
gcc -c outlib.c
[***@localhost testmake]$ ls
DandL.txt Makefile outlib.c outlib.h outlib.o testmake.c
其中,make all和make clean是Makefile最常用的功能,一般要注意检查一下。
当你掌握这些~你已经出师可以自己写Makefile~当然,还有许多高端的技巧等你学习探索。