- 使用MakeFile.env 控制
########### MakeFile.env ##########
CC=gcc
CPP=g++
MAKE=make
AR=ar cr
RM = -rm -rf
ifeq ($(DEBUG), y)
CFLAGS += -g
CPPLAGS += -g
else
CFLAGS += -O2
CPPLAGS += -O2
endif
CFLAGS+=-Wall
CPPLAGS+=-Wall
#根目录路径
#TOPDIR
#目标文件所在的目录
OBJS_DIR = build/obj
dirs:=
(
s
h
e
l
l
f
i
n
d
.
−
m
a
x
d
e
p
t
h
1
−
t
y
p
e
d
)
d
i
r
s
:
=
(shell find . -maxdepth 1 -type d) dirs:=
(shellfind.−maxdepth1−typed)dirs:=(basename
(
p
a
t
s
u
b
s
t
.
/
(patsubst ./%,%,
(patsubst./(dirs)))
dirs:=$(filter-out
(
e
x
c
l
u
d
e
d
i
r
s
)
,
(exclude_dirs),
(excludedirs),(dirs))
SUBDIRS := $(dirs)
CSRCS=
(
w
i
l
d
c
a
r
d
∗
.
c
)
C
O
B
J
S
=
(wildcard *.c) COBJS=
(wildcard∗.c)COBJS=(CSRCS:%.c=%.o)
CDEPENDS=$(CSRCS:%.c=%.d)
CPPSRCS=
(
w
i
l
d
c
a
r
d
∗
.
c
p
p
)
C
P
P
O
B
J
S
=
(wildcard *.cpp) CPPOBJS=
(wildcard∗.cpp)CPPOBJS=(CPPSRCS:%.cpp=%.o)
CPPDEPENDS=$(CPPSRCS:%.cpp=%.d)
#subdirs 设置子目录作为目标
all:$(TARGET) $(LIB) subdirs
#模块输出的链接库放在LIBPATH目录下
(
L
I
B
)
:
(LIB):
(LIB):(COBJS) $(CPPOBJS)
$(AR) $@ $^
cp $@ $(LIBPATH)
#模块输出的可执行文件放在EXEPATH目录下,生成TARGET还依赖其他子目录先编译
(
T
A
R
G
E
T
)
:
(TARGET):
(TARGET):(COBJS) $(CPPOBJS) subdirs
$(CC) -o $@ $^ $(LDFLAGS)
cp $@ $(EXEPATH)
#进入指定子目录,并执行子目录的Makefile文件
subdirs:$(SUBDIRS)
for dir in $(SUBDIRS);
do $(MAKE) -C $$dir all||exit 1;
done
$(warning, “here add the debug info”)
KaTeX parse error: Expected 'EOF', got '#' at position 18: …OBJS):%.o:%.c #̲(CC) -c $< -o $@ $(CFLAGS)
$(CC) -c $< -o
(
T
O
P
D
I
R
)
/
(TOPDIR)/
(TOPDIR)/(OBJS_DIR)$@ $(CFLAGS)
KaTeX parse error: Expected 'EOF', got '#' at position 22: …JS):%.o:%.cpp #̲(CC) -c $< -o $@ $(CPPFLAGS)
$(CC) -c $< -o
(
T
O
P
D
I
R
)
/
(TOPDIR)/
(TOPDIR)/(OBJS_DIR)$@ $(CPPFLAGS)
-include $(CDEPENDS) $(CPPDEPENDS)
$(CDEPENDS):%.d:%.c
set -e; rm -f $@;
$(CC) -MM $(CFLAGS) $< >
@
.
@.
@.$KaTeX parse error: Expected 'EOF', got '\ ' at position 3: ; \̲ ̲ sed 's,\($*\)\…KaTeX parse error: Can't use function '$' in math mode at position 4: > $̲@; \ rm $@.$$
$(CPPDEPENDS):%.d:%.cpp
set -e; rm -f $@;
$(CC) -MM $(CPPFLAGS) $< >
@
.
@.
@.$KaTeX parse error: Expected 'EOF', got '\ ' at position 3: ; \̲ ̲ sed 's,\($*\)\…KaTeX parse error: Can't use function '$' in math mode at position 4: > $̲@; \ rm $@.$$
clean:
@rm
(
T
O
P
D
I
R
)
/
(TOPDIR)/
(TOPDIR)/(OBJS_DIR)/.o
@rm -rf
(
T
O
P
D
I
R
)
/
(TOPDIR)/
(TOPDIR)/(BIN_DIR)/
for dir in $(SUBDIRS);
do $(MAKE) -C $$dir clean||exit 1;
done
$(RM) $(TARGET) $(LIB) $(COBJS) $(CDEPENDS) $(CPPOBJS) $(CPPDEPENDS)
2. 下级目录makefile
3.TOPDIR=./…/…/…
BUILD_SRC_DIR=./…/…
CUR_DIR:= .
TARGET=demo
exclude_dirs= include
EXEPATH=$(TOPDIR)/build/bin
INCPATH=-IKaTeX parse error: Expected 'EOF', got '\ ' at position 28: …party/include/ \̲ ̲ -I(BUILD_SRC_DIR)/include/
-I$(BUILD_SRC_DIR)/include/common \
CFLAGS= $(INCPATH)
CPPFLAGS= $(INCPATH)
LIBS_PATH = -L$(TOPDIR)/build/lib \
LIBS=
LDFLAGS = ( L I B S P A T H ) L D F L A G S + = (LIBS_PATH) LDFLAGS += (LIBSPATH)LDFLAGS+=(LIBS)
include $(TOPDIR)/makefile/Makefile.env