多文件夹makefile

多文件夹Make的例子:

LibDir = ../../export
ModuleLib =

ModuleDir = ../../

TestCommonDir = ../common

IncludeDir = -I$(TestCommonDir) \
                -I$(ModuleDir)/include/base \
    
app = myapp

CC = gcc
ECHO = echo
RM = rm -rf

CFLAGS += -DINSTALL_TYPE   $(IncludeDir)

CFLAGS += -pthread  -g  -Wextra
CFLAGS += -fprofile-arcs -ftest-coverage
LDFLAGS += -fprofile-arcs -ftest-coverage

Srcs_c  =  $(wildcard *.c)  
Srcs_c  += $(wildcard $(TestCommonDir)/*.c)
Srcs_obj  = $(patsubst %.c, %.co, $(Srcs_c))
Srcs_gcno  = $(patsubst %.c, %.gcno, $(Srcs_c))
Srcs_gcda  = $(patsubst %.c, %.gcda, $(Srcs_c))

all: $(app)

%.co:%.c
    $(ECHO) Compiling  Src file:$(notdir $<)...
    $(CC) $(CFLAGS) -c $< -o $@

$(app) : $(ModuleLib) $(Srcs_obj)
    $(ECHO) linking target $@
    $(CC) -lpthread -lm -lrt -lgcov  $(LDFLAGS) -o $@ -Wl,--start-group $^ $(ModuleLib) -Wl,--end-group
$(ModuleLib) :
    $(RM) $(ModuleLib)
    cd $(ModuleDir)/make/ && ./my.sh
clean :
    $(RM) $(Srcs_obj) $(Srcs_gcno) $(Srcs_gcda) $(app) *.log core*

.PHONY: all clean $(ModuleLib)
======================================================================
单文件夹的makefile,需要使用notdir函数来去除路径:

Srcs_c = $(notdir $(wildcard *.c ))
Srcs_cpp = $(notdir $(wildcard *.cpp))
Srcs_obj = $(patsubst %.c,%.co, $(Srcs_c)) $(patsubst %.cpp,%.cppo,$(Srcs_cpp))
Srcs_gcno = $(patsubst %.c,%.gcno, $(Srcs_c)) $(patsubst %.cpp,%.gcno,$(Srcs_cpp))
Srcs_gcda = $(patsubst %.c,%.gcda, $(Srcs_c)) $(patsubst %.cpp,%.gcda,$(Srcs_cpp))

posted @ 2017-12-29 22:44  elseliving的记录  阅读(216)  评论(0编辑  收藏  举报