I’m developing a project that includes multiple static libraries. The way in which I build them is always the same. I would like to know how I can reuse these rules for all the libraries and avoid doing copy & paste for each one. Here is an example for building a ‘foo’ library.
# inputs
#
FOO_LIB_NAME := libfoo.a
FOO_SRC_DIR := $(SRC_DIR)/foo
FOO_SRC_FILES := \
foo_file1.cc \
subdir/foo_file2.cc \
subdir/foo_file3.cc \
...
FOO_CFLAGS :=
# rules
#
FOO_SRC_FILES := $(addprefix $(FOO_SRC_DIR)/,$(FOO_SRC_FILES))
FOO_OBJ_FILES := $(subst .cc,.o,$(FOO_SRC_FILES))
$(FOO_LIB_NAME): $(FOO_OBJ_FILES)
$(AR) rc $@ $^
$(FOO_OBJ_FILES): %.o: %.cc
$(CXX) $(COMMON_CFLAGS) $(FOO_CFLAGS) $< -o $@
Additionally, if you have any comment about the previous excerpt of code I would be glad to know about it. I’m quite new at writing Makefiles.
Thanks in advance
You can accomplish this with templates and the
evalfunction in Make. The GNU Make Manual has examples in the section onevalHere is the example from the manual: