i007.cc

i007.cc

优先队列-降维打击

Makefile遍历编译文件夹下所有.c文件  

2013-03-22 00:06:51|  分类: linux|举报|字号 订阅

  1. TARGET:=test
  2. CC:=gcc
  3. FILES=`ls *.c`
  4. all: $(TARGET)
  5. $(TARGET): *.o
  6. $(CC) -o $@ $^
  7. clean:
  8. -rm $(TARGET) *.o
  9. define compile_c_file
  10. @for file in $(FILES); do \
  11. ( echo “$(CC) -c $$file” && $(CC) -c $$file ) \
  12. done;
  13. endef
  14. %.o: %.c
  15. $(call compile_c_file)
 
 

制作一个遍历当前子目录的Makefile

要对子目录执行make,需要在当前目录制作一个Makefile,遍历所有子目录的Makefile,并运行相应的make target. 以下是我用来编译内核模块的一个Makefile

#

# Reference http://www.gnu.org/software/make/manual/make.html

#

需要排除的目录

exclude_dirs := include bin

取得当年子目录深度为1的所有目录名称

dirs := $(shell find . -maxdepth 1 -type d)

dirs := $(basename $(patsubst ./%,%,$(dirs)))

dirs := $(filter-out $(exclude_dirs),$(dirs))

避免clean子目录操作同名,加上_clean_前缀

SUBDIRS := $(dirs)

clean_dirs := $(addprefix _clean_,$(SUBDIRS) )

#

.PHONY: subdirs $(SUBDIRS) clean

执行默认make target

$(SUBDIRS):    

$(MAKE) -C $@

subdirs: $(SUBDIRS)

执行clean

$(clean_dirs):    

$(MAKE) -C $(patsubst _clean_%,%,$@) clean

clean: $(clean_dirs)    

@find . /        

/( -name ‘*.[oas]’ -o -name ‘*.ko’ -o -name ‘.*.cmd’ /

-o -name ‘.*.d’ -o -name ‘.*.tmp’ -o -name ‘*.mod.c’ /

-o -name ‘*.symtypes’ /) /

-type f -print | xargs rm -f

发表回复