今天学了嵌套makefile,在这里简单讲解一下。
新建一个文件夹Multi_Makefile_test(mkdir Multi_Makefile_test),里面新建两个文件夹(mkdir 1f 2f)和新建两个文件(touch main.c Makefile)。文件夹1f里新建两个文件(touch hello1.c Makefile),同样文件夹2f里也新建两个文件(touch hello2.c Makefile)。
添加代码如下
//Multi_Makefile_test文件夹中的main.c
#include<stdio.h>
int main()
{
hello1();
hello2();
}
//Multi_Makefile_test文件夹中的Makefile
obj=main.o 1f/hello1.o 2f/hello2.o
edit : $(obj)
gcc -o edit main.o 1f/hello1.o 2f/hello2.o
main.o : main.c
gcc -c main.c
hello1.o : hello1.c
$(MAKE) -C 1f
hello2.o : hello2.c
$(MAKE) -C 2f
.PHONY : clean
clean :
-rm edit main.o -fr
-rm 1f/*.o -fr
-rm 2f/*.o -fr
//2f文件夹中的hello2.c
#include<stdio.h>
void hello2()
{
printf("hello2\n");
}
//2f文件夹中的Makefile
hello2.0:hello2.c
gcc -o hello2.o hello2.c
//1f文件夹中的hello1.c
#include<stdio.h>
void hello1()
{
printf("hello1\n");
}
//1f文件夹中的Makefile
hello1.0:hello1.c
gcc -o hello1.o hello1.c
http://pan.baidu.com/s/1gfsNBvP 工程
补充:当需要生成多个可执行程序时,需要在Makefile中把目标用all(all:edit1 edit2)来定义,再去编写完成各个小目标的规则(edit1:xxx edit2:xxxx)。