Makefile遍历当前⽬录源⽂件及其⼦⽬录下源⽂件之前学的Makefile 不⽤的话经常会忘掉⼀些规则,记录下⼀些常⽤的功能的随笔,⽅便查。
先来看⼀个Makefile⽂件,通过 foreach 和 patsubst ⽅法和 ^、$< 变量来构建遍历的编译
CROSS =
CC = $(CROSS)gcc
CXX = $(CROSS)g++
DEBUG = -g
CFLAGS = $(DEBUG) -Wall -c -fPIC
MV = mv -f
RM = rm -rf
LN = ln -sf
TARGET = libhello.so
makefile phony
TOP_PATH = $(shell pwd)
INC_PATH = $(TOP_PATH)/include
SRC_PATH = $(TOP_PATH)
SRC_PATH += $(TOP_PATH)/src
MOD_PATH = $(TOP_PATH)/modules
MOD_LIB_PATH = $(MOD_PATH)/lib
MOD_INC_PATH = $(MOD_PATH)/include
DIRS = $(shell find $(SRC_PATH) -maxdepth 3 -type d)
FILES = $(foreach dir, $(DIRS), $(wildcard $(dir)/*.cpp))
>>>>>>>>>>>###
# modules
>>>>>>>>>>>###
modules =
MODULES_PATH = $(foreach m, $(modules), $(MOD_PATH)/$(m))
>>>>>>>>>>>###
# srcs
>>>>>>>>>>>###
SRCS_CPP += $(foreach dir, $(DIRS), $(wildcard $(dir)/*.cpp))
SRCS_CC += $(foreach dir, $(DIRS), $(wildcard $(dir)/*.cc))
SRCS_C += $(foreach dir, $(DIRS), $(wildcard $(dir)/*.c))
>>>>>>>>>>>###
# objs
>>>>>>>>>>>###
OBJS_CPP = $(patsubst %.cpp, %.o, $(SRCS_CPP))
OBJS_CC = $(patsubst %.cc, %.o, $(SRCS_CC))
OBJS_C = $(patsubst %.c, %.o, $(SRCS_C))
>>>>>>>>>>>###
# paths
>>>>>>>>>>>###
INC_PATH += -I$(MOD_INC_PATH)
INC_PATH += -I$(MOD_PATH)
LIB_PATH += -L$(TOP_PATH)/lib
LIB_PATH += -L$(MOD_LIB_PATH)
>>>>>>>>>>>###
# libs
>>>>>>>>>>>###
>>>>>>>>>>>###
# building
>>>>>>>>>>>###
all:$(TARGET)
$(TARGET) : $(OBJS_CPP) $(OBJS_CC) $(OBJS_C)
@ for i in $(MODULES_PATH); \
do \
make -C $$i; \
done

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论