天嵌 ARM开发社区

 找回密码
 注册
查看: 1689|回复: 1

Makefile的探讨!有兴趣的请进!

[复制链接]
cumtwys 发表于 2010-10-19 16:36:00 | 显示全部楼层 |阅读模式
本帖最后由 cumtwys 于 2010-10-19 16:54 编辑

对于刚学Makefile的同学一定有我这样的疑惑!就是make的嵌套编译的问题!
举个例子:我有一个主目录test/,其下有一个子目录subdir/。test/目录下有一个main.c和一个主Makefile文件,subdir/目录下有input.cdisplay.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文件呢!?高手请回答!谢谢!一直搞不清楚嵌套怎么用!
下面是目录截图:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
 楼主| cumtwys 发表于 2010-10-19 16:54:51 | 显示全部楼层
我编辑这篇帖子不容易!高手看了要回答我啊!我在线等!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号-2 )

GMT+8, 2024-9-18 04:21 , Processed in 1.047785 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表