|
本帖最后由 cumtwys 于 2010-10-19 16:54 编辑
对于刚学Makefile的同学一定有我这样的疑惑!就是make的嵌套编译的问题!
举个例子:我有一个主目录test/,其下有一个子目录subdir/。test/目录下有一个main.c和一个主Makefile文件,subdir/目录下有input.c和display.c两个c文件,还有一个子Makefile文件。
test/目录内容如下:
//main.c如下很简单的
#include <stdio.h>
int main(void)
{
int x,y;
input(&x,&y);
display(x,y);
return 0;
}
//主Makefile如下
VPATH=/root/test/subdir/
export obj = main.o input.o display.o
rmove = $(wildcard *.o) *~
all : subsystem main
subsystem:
cd subdir && $(MAKE)
main : $ (obj)
cc -o main $^
main.o:
gcc -c main.c
.PHONY:cleanall cleandiff
cleanall:cleandiff
-rm -f main
cleandiff:
-rm -f $(rmove)
-rm -f /root/test/subdir/*.o
-rm -f /root/test/subdir/*~
subdir/目录内容如下:
//input.c内容如下
#include <stdio.h>
void input(int *x,int *y)
{
scanf("%d,%d",x,y);
}
//display.c内容如下
#include <stdio.h>
void display(int x,int y)
{
int a;
a=x*y/(x+y);
printf("The x is %d , the y is %d \n",x,y);
printf("The x*y/(x+y) is %d \n",a);
}
//子Makefile内容如下
object =input.o display.o
all : $(object)
$(object):input.c display.c
gcc -c $^
………………………………………………………………………………………………………………
在进入test目录后执行:make
输出如下信息:
[root@localhost test]# make
cd subdir && make
make[1]: Entering directory `/root/test/subdir'
gcc -c input.c display.c
make[1]: Leaving directory `/root/test/subdir'
gcc -c main.c
cc -c -o input.o /root/test/subdir/input.c
cc -c -o display.o /root/test/subdir/display.c
gcc -o main main.o input.o display.o
我纳闷的就是我在子目录中的Makefile已经编译出了input.o和display.o文件,为什么返回主目录后又编译了一遍input.o和display.o文件,而且在主目录下多了这两个文件。为什么主Makefile不是直接调用子目录subdir下的.o文件呢!?高手请回答!谢谢!一直搞不清楚嵌套怎么用!
下面是目录截图: |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
×
|