文件层级结构:
│ Makefile
│ tmp
│
├─include
│ public.h
│
└─src
├─moda
│ Makefile
│ moda.c
│ moda.h
│
└─modb
Makefile
modb.c
modb.h
public.h
#define MAX_SIZE 10
#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 = gcc ROOT := $(shell pwd) INCLUDE := $(ROOT)/include SRC := $(ROOT)/src USR_SUB_DIR := $(SRC)/moda $(SRC)/modb default:usr usr: @for n in $(USR_SUB_DIR); do $(MAKE) -C $$n ; done clean: @for n in $(USR_SUB_DIR); do $(MAKE) -C $$n clean; done
CC = gcc ROOT := $(shell pwd)/../.. INCLUDE := $(ROOT)/include CFLAGS += -I$(INCLUDE) target:moda modb:moda.o $(CC) -o moda moda.o modb.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.c gcc moda.o -o moda make[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.o make[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 总用量 5 drwxrwxrwx 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.o linux:/mnt/hgfs/vmware-share/makefile/src/moda # ./moda This is mod A, MAX_SIZE:10linux:/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‘
多层级的makefile编写——递归调用makefile,布布扣,bubuko.com
原文:http://blog.csdn.net/wangyuling1234567890/article/details/22111271