How can I run a template rule in another rule in a makefile?

I am looking to write a makefile to automate the compilation of a project I am working on, where files may or may not change in number. I also need to be able to quickly say make to compile the files as a debug build or release build (differentiated using the command line definition). After some research, I came up with rule patterns and made one. Here is the code that I still have:

# Our folders
# ODIR -    The .o file directory
# CDIR -    The .cpp file directory
# HDIR -    The .hpp file directory
ODIR = obj
CDIR = src
HDIR = inc

# Get our compiler and compiler commands out of the way
# CC -  Our compiler
# CFNO - Our compiler flags for when we don't have an output file
# CF -  Our compiler flags. This should be appended to any compile and should
#           have the name of the output file at the end of it.
# OF -  Object flags. This should be appended to any line that is generating
#           a .o file. 
CC = g++
CFNO = -std=c++11 -wall -Wno-write-strings -Wno-sign-compare -lpaho-mqtt3c -pthread -O2 -I$(HDIR)
CF = $(CFNO) -o
OF = -c

# Our project/output name
NAME = tls_test

# Set out Debug and Release settings, as well as any defines we will use to identify which mode we are in
# NOTE: '-D[NAME OF DEFINE]' is how you create a define through the compile commands
DEBUG = -DDEBUG -g
RELEASE = -DRELEASE

# Our two compile modes
# eval allows us to create variables mid-rule
debug:
    $(eval DR = $(DEBUG))

release:
    $(eval DR = $(RELEASE))

# Our compiling commands
all: 
    $(CC) $(CF) $(NAME) $(ODIR)/*.o

# NOTE: $@ is the end product being created and $< is the first of the prerequisite
$(ODIR)/%.o: $(CDIR)/%.c
    echo "$(CC) $(DR) $(OF) $(CF) $@ $<"

, , , , . make debug, release, make, . , , . , ?

+4
1

, , , . , .

(-MMD -MP), .

ODIR := obj
CDIR := src
HDIR := inc

SRCS := $(wildcard $(CDIR)/*.cpp)
OBJS := $(SRCS:$(CDIR)/%.cpp=$(ODIR)/%.o)
DEPS := $(OBJS:%.o=%.d)

CPPFLAGS := -I$(HDIR) -MMD -MP
CXXFLAGS := -std=c++11 -Wall -Wno-write-strings -Wno-sign-compare -pthread -O2
LDFLAGS  := -pthread
LDLIBS   := -lpaho-mqtt3c

NAME := tls_test

.PHONY: debug release clean

debug: CPPFLAGS+=-DDEBUG
debug: CXXFLAGS+=-g

release: CPPFLAGS+=-DRELEASE

debug release: $(NAME)

$(NAME): $(OBJS)
    $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

$(OBJS): $(ODIR)/%.o: $(CDIR)/%.cpp
    $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(OUTPUT_OPTION) $<

clean: ; $(RM) $(NAME) $(OBJS) $(DEPS)    

-include $(DEPS)
+3

All Articles