调试通过的自动化 Makefile 参考

Makefile实现:

1 指定输出 *.o 和 target 路径,并且app下  *c *.cpp 文件目录映射 *.o 输出目录

2 c 和 c++ 源码混合编译

3 静态模式规则编译

# $(realpath $(dir $(lastword $(MAKEFILE_LIST))))  ->  /xxxx/unit_test/test_wirelessscan
CURR_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))

# $(realpath $(dir $(lastword $(MAKEFILE_LIST)))../../)  ->  /xxxx
FATH_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))../../)

# $(subst $(FATH_DIR)/, , $(CURR_DIR))  ->  unit_test/test_wirelessscan
RELA_DIR := $(strip $(subst $(FATH_DIR)/, , $(CURR_DIR)))
OBJ_DIR := $(strip $(OUTPUT)/objs/$(RELA_DIR))
TAR_DIR := $(OUTPUT)/bin

LOCAL_TARGET	:= test_wirelessscan

INCLUDE := -I$(CURR_DIR)/include
EXTLIBS := -Wl,-Bdynamic -L$(CURR_DIR)/lib -liw -lsqlite3 -lsqlitewrapper -liwscaninfo
EXTLIBS += -Wl,-Bdynamic -lstdc++ -ldl -lrt -lpthread
CFLAGS := -g -Wall -O2
LDFLAG := 
# for share lib (*.so) compile
#CFLAGS += -fPIC
#LDFLAG += -shared

SRC_DIR := . test

# $(foreach dir, $(SRC_DIR), $(wildcard $(CURR_DIR)/$(dir)/*.c $(CURR_DIR)/$(dir)/*.cpp))  ->  /xxxx/unit_test/test_wirelessscan/test1.cpp /xxxx/unit_test/test_wirelessscan/test/test2.cpp
# $(foreach dir, $(SRC_DIR), $(subst $(CURR_DIR)/, , $(wildcard $(CURR_DIR)/$(dir)/*.c $(CURR_DIR)/$(dir)/*.cpp)))  ->  test1.cpp test/test2.cpp
SRCS_ALL := $(foreach dir, $(SRC_DIR), $(subst $(CURR_DIR)/, , $(wildcard $(CURR_DIR)/$(dir)/*.c $(CURR_DIR)/$(dir)/*.cpp)))

OBJS_C := $(filter %.o, $(patsubst %.c, $(OBJ_DIR)/%.o, $(SRCS_ALL)))

# $(filter %.o, $(patsubst %.c, $(OBJ_DIR)/%.o, $(SRCS_ALL)))  ->  /xxxx/output/objs/unit_test/test_wirelessscan/test1.o /xxxx/output/objs/unit_test/test_wirelessscan/test/test2.o
OBJS_CPP := $(filter %.o, $(patsubst %.cpp, $(OBJ_DIR)/%.o, $(SRCS_ALL)))

$(OBJS_C): $(OBJ_DIR)/%.o : $(CURR_DIR)/%.c
	$(call create-dir, $(dir $@))
	$(Q)$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@

# $(Q)$(CP) $(CFLAGS) -std=gnu++11 $(INCLUDE) -c /xxxx/unit_test/test_wirelessscan/test1.cpp -o /xxxx/output/objs/unit_test/test_wirelessscan/test1.o
# $(Q)$(CP) $(CFLAGS) -std=gnu++11 $(INCLUDE) -c /xxxx/unit_test/test_wirelessscan/test/test2.cpp -o /xxxx/output/objs/unit_test/test_wirelessscan/test/test2.o
$(OBJS_CPP): $(OBJ_DIR)/%.o : $(CURR_DIR)/%.cpp
	@mkdir -p $(dir $@)
	$(Q)$(CP) $(CFLAGS) -std=gnu++11 $(INCLUDE) -c $< -o $@

$(LOCAL_TARGET): $(TAR_DIR)/$(LOCAL_TARGET)
$(TAR_DIR)/$(LOCAL_TARGET): $(OBJS_C) $(OBJS_CPP)
	$(call create-dir, $(TAR_DIR))
	$(Q)$(CP) $^ -o $@ $(EXTLIBS) $(LDFLAG)
	$(call info-g," Build $@ Done.")

$(call add-target-into-build, $(LOCAL_TARGET))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值