多层级的makefile编写——递归调用makefile

来源:互联网 发布:小米电视怎么设置网络 编辑:程序博客网 时间:2024/05/22 17:31

文件层级结构:

│  Makefile 
│  tmp 
 
├─include 
│      public.h 
 
└─src 
    ├─moda 
    │      Makefile 
    │      moda.c 
    │      moda.h 
    │ 
    └─modb 
            Makefile 
            modb.c 
            modb.h

public.h 

#define MAX_SIZE 10
moda.c   (modb.c类似) 
#include <stdio.h>#include "moda.h"#include "public.h"int main(void){  printf("This is mod A, MAX_SIZE:%d \n", MAX_SIZE);  return 0;}

顶层makefile:

CC = gccROOT := $(shell pwd)INCLUDE := $(ROOT)/includeSRC := $(ROOT)/srcUSR_SUB_DIR := $(SRC)/moda $(SRC)/modbdefault:usrusr:  @for n in $(USR_SUB_DIR); do $(MAKE) -C $$n ; done  clean:  @for n in $(USR_SUB_DIR); do $(MAKE) -C $$n clean; done

moda目录下makefile 
CC = gccROOT := $(shell pwd)/../..INCLUDE := $(ROOT)/includeCFLAGS += -I$(INCLUDE)target:modamodb:moda.o  $(CC) -o moda moda.omodb.o:moda.c  $(CC) $(CFLAGS) -c moda.c   clean:  -rm *.o moda
执行:
linux:/mnt/hgfs/vmware-share/makefile # make make[1]: Entering directory `/mnt/hgfs/vmware-share/makefile/src/moda'gcc -I/mnt/hgfs/vmware-share/makefile/src/moda/../../include   -c -o moda.o moda.cgcc   moda.o   -o modamake[1]: Leaving directory `/mnt/hgfs/vmware-share/makefile/src/moda'make[1]: Entering directory `/mnt/hgfs/vmware-share/makefile/src/modb'gcc -I/mnt/hgfs/vmware-share/makefile/src/modb/../../include -c modb.c gcc -o modb modb.omake[1]: Leaving directory `/mnt/hgfs/vmware-share/makefile/src/modb'linux:/mnt/hgfs/vmware-share/makefile #linux:/mnt/hgfs/vmware-share/makefile # cd src/moda/linux:/mnt/hgfs/vmware-share/makefile/src/moda # ll总用量 5drwxrwxrwx  1 root root    0 2014-03-25 23:51 .drwxrwxrwx  1 root root    0 2014-03-25 23:10 ..-rwxrwxrwx  1 root root  219 2014-03-25 23:30 Makefile-rwxrwxrwx  1 root root 6803 2014-03-25 23:51 moda-rwxrwxrwx  1 root root  149 2014-03-25 23:39 moda.c-rwxrwxrwx  1 root root    0 2014-03-25 23:10 moda.h-rwxrwxrwx  1 root root  908 2014-03-25 23:51 moda.olinux:/mnt/hgfs/vmware-share/makefile/src/moda # ./modaThis is mod A, MAX_SIZE:10
linux:/mnt/hgfs/vmware-share/makefile #linux:/mnt/hgfs/vmware-share/makefile #linux:/mnt/hgfs/vmware-share/makefile #
linux:/mnt/hgfs/vmware-share/makefile # make clean 
make[1]: Entering directory `/mnt/hgfs/vmware-share/makefile/src/moda' 
rm *.o moda 
make[1]: Leaving directory `/mnt/hgfs/vmware-share/makefile/src/moda' 
make[1]: Entering directory `/mnt/hgfs/vmware-share/makefile/src/modb' 
rm *.o modb 
make[1]: Leaving directory `/mnt/hgfs/vmware-share/makefile/src/modb'
0 0
原创粉丝点击