码迷,mamicode.com
首页 > 其他好文 > 详细

自动编译当前目录下所有文件的Makefile

时间:2017-05-02 13:45:21      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:compile   type   项目   car   rod   find   pre   bin   path   

下面是我在一个项目中使用的Makefile.

脚本会自动搜索当前目录下所有子目录,并依据目录下的.c 和 .cxx生成对应的.o,最后生成应用application,

代码中删除了项目相关配置,如CFLAGS,LDFLAGS,CXXFLAGS中的gcc配置

欢迎转载,烦请添加链接,谢谢!

 1 SHELL=/bin/sh
 2 
 3 CC = $(CROSS_COMPILE)gcc
 4 CXX = $(CROSS_COMPILE)g++
 5 LD = $(CROSS_COMPILE)ld
 6 
 7 CFLAGS +=
 8 
 9 LDFLAGS +=-L.
10 
11 MAKE_DIR=$(PWD)
12 
13 CFLAGS += $(INCLS)
14 CXXFLAGS += $(INCLS)
15 #find all the sub-directory
16 VPATH=$(foreach dir,$(shell find . -type d),$(shell echo $(dir):))
17 #include all the sub-directory
18 INCLS=$(foreach dir,$(subst :, ,$(VPATH)),$(shell echo -I$(dir)))
19 #find all .c and produce .o
20 C_SRC_PATH = $(foreach dir,$(subst :, ,$(VPATH)),$(wildcard $(dir)/*.c))
21 COBJS = $(subst .c,.o,$(C_SRC_PATH))
22 #find all .cxx and produce .o
23 CPP_SRC_PATH = $(foreach dir,$(subst :, ,$(VPATH)),$(wildcard $(dir)/*.cxx))
24 CPPOBJS = $(subst .cxx,.o,$(CPP_SRC_PATH))
25 
26 execobjs = $(COBJS) $(CPPOBJS)
27 #the scripts for compile
28 .PHONY: all
29 
30 all: $(execobjs)
31     $(CXX) -o application $(execobjs) $(LDFLAGS)
32 
33 $(COBJS):%.o:%.c
34     $(CC) -c $< -o $@ $(CFLAGS)
35 
36 $(CPPOBJS):%.o:%.cc
37     $(CC) -c $< -o $@ $(CXXFLAGS)
38 #the scripts for clean
39 .PHONY: clean
40 
41 clean:
42     rm application $(execobjs)

 

自动编译当前目录下所有文件的Makefile

标签:compile   type   项目   car   rod   find   pre   bin   path   

原文地址:http://www.cnblogs.com/marf/p/6795377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!