编写包含多文件的Makefile以及Makefile的嵌套实验

看到一篇讲Makefile嵌套的文章,刚好看到linux1.0内核Makefile中有.EXPORT_ALL_VARIABLES:,因此也学习一下.EXPORT_ALL_VARIABLES:的使用。

编写包含多文件的Makefile以及Makefile的嵌套实验 - 北极星 - xiebingsuccess的博客

 输入"tree"命令,查看目录;若没有则安装tree命令则 (sudo apt-get install tree )

[root@localhost f1]# cat f1.c

//f1.c

#include "../include/myinclude.h"

void print1()

{

printf("Message f1.c\n");

return;

}

[root@localhost f1]# cat Makefile 

#Makefile (/f1目录下的)

 

../$(OBJS_DIR)/f1.o:f1.c                                                                                     

    $(CC) -c $^ -o $@

[root@localhost f1]# 

f2.c 和f2目录下的Makefle和f1目录下的除名字外内容相同

编写包含多文件的Makefile以及Makefile的嵌套实验 - 北极星 - xiebingsuccess的博客

 [root@localhost makefilestudy]# cat Makefile

#顶层目录下的Makeflie文件

cc=gcc                                                                                                       

SUBDIRS=f1 \

        f2 \

        main \

        obj

OBJS=f1.o f2.o main.o

BIN=myapp

OBJS_DIR=obj

BIN_DIR=bin

###export CC OBJS BIN OBJS_DIR BIN_DIR

.EXPORT_ALL_VARIABLES:

all:CHECK_DIR $(SUBDIRS)

CHECK_DIR:

    mkdir -p $(BIN_DIR)

$(SUBDIRS):ECHO

    make -C $@

ECHO:

    @echo $(SUBDIRS)

    @echo begin compile

CLEAN:

    @$(RM) $(OBJS_DIR)/*.o

    @rm -rf $(BIN_DIR)

[root@localhost makefilestudy]# cat include/myinclude.h 

//include目录下的myinclude.h 文件  

#include <stdio.h>  

[root@localhost makefilestudy]# cat main/

main.c    Makefile  

[root@localhost makefilestudy]# cat main/main.c 

//main.c 

 

#include <stdio.h>                                                                                           

int main()

{

    print1();

    print2();

 

    return 0;

 }

[root@localhost makefilestudy]# cat main/Makefile 

#Makefile (/main目录下的Makefile)  

  

../$(OBJS_DIR)/main.o:main.c                                                                                   

    $(CC) -c $^ -o $@  

[root@localhost makefilestudy]# cat obj/Makefile 

#Obj目录下的Makefile  

  

../$(BIN_DIR)/$(BIN):$(OBJS)                                                                                   

    $(CC) -o $@ $^

[root@localhost makefilestudy]# 

 

[root@localhost makefilestudy]# make

mkdir -p bin

f1 f2 main obj

begin compile

make -C f1

make[1]: Entering directory `/mnt/mywork/makefilestudy/f1'

cc -c f1.c -o ../obj/f1.o

make[1]: Leaving directory `/mnt/mywork/makefilestudy/f1'

make -C f2

make[1]: Entering directory `/mnt/mywork/makefilestudy/f2'

cc -c f2.c -o ../obj/f2.o

make[1]: Leaving directory `/mnt/mywork/makefilestudy/f2'

make -C main

make[1]: Entering directory `/mnt/mywork/makefilestudy/main'

cc -c main.c -o ../obj/main.o  

make[1]: Leaving directory `/mnt/mywork/makefilestudy/main'

make -C obj

make[1]: Entering directory `/mnt/mywork/makefilestudy/obj'

cc -o ../bin/myapp f1.o f2.o main.o

make[1]: Leaving directory `/mnt/mywork/makefilestudy/obj'

[root@localhost makefilestudy]# 

编写包含多文件的Makefile以及Makefile的嵌套实验 - 北极星 - xiebingsuccess的博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值