Well, you're mixing up a lot of things.
Let's clean this up and keep only what is needed :
EXE := Tests
SRC_DIR := .
OBJ_DIR := obj
SRC := $(wildcard $(SRC_DIR)/*.cpp)
OBJ := $(SRC:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.o)
CPPFLAGS := -I/home/mg/DS-5-Workspace/Tests/include
CPPFLAGS += -MMD -MP -D_THREAD_SAFE
CXXFLAGS := -W -Wall -Wno-deprecated -pedantic -O3 -g
LDFLAGS := -L/home/mg/DS-5-Workspace/Tests/cpputest/lib
LDFLAGS += -Wl,-rpath,.
LDLIBS := -lCppUTestExt -lCppUTest -lm -lstdc++ -lpthread -ldl
.PHONY: all clean fclean re
all: $(EXE)
clean:
$(RM) -f -r $(OBJ_DIR)
fclean: clean
$(RM) -f $(EXE)
re: fclean all
$(EXE): $(OBJ)
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
# %.a: $(OBJ)
# $(AR) crvs $@ $^
# ranlib $@
# %.so: CXXFLAGS += -fPIC
# %.so: $(OBJ)
# $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
$(OBJ_DIR):
@mkdir -p $@
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $<
-include $(OBJ:.o=.d)
Some explanations :
Avoid the
$(shell ...)
function, because it'll be executed each time the variable is called if assigned with the=
operator instead of:=
operator.$(CC)
is a built-in variable containingcc
orgcc
(should be equivalent). Use the built-in$(CXX)
to useg++
.-g
,-pedantic
,-O3
,-Wno-deprecated
and-Wall
are compiler flags, they should be in theCFLAGS
(for C) orCXXFLAGS
(for C++) built-in variables.-I <path>
and-D_THREAD_SAFE
are preprocessor flag, thus should be in theCPPFLAGS
built-in variable.-MMD -MP
will auto-generate dependency files (.d
extension) for each.o
file. You can read more here..cpp.o:
is a suffix rule, and suffix rules are the old-fashioned way of defining implicit rules for make. You should just rely upon these implicit rules make already know about or make your own the modern way.You don't need to define
.SUFFIXES:
by yourself for such widely used targets. The variable SUFFIXES is defined to the default list of suffixes before make reads any makefiles. Make 3.82 defines these suffixes by default :.SUFFIXES: .out .a .ln .o .c .cc .C .cpp .p .f .F .m .r .y .l .ym .yl .s .S .mod .sym .def .h .info .dvi .tex .texinfo .texi .txinfo .w .ch .web .sh .elc .el
If you have any questions, go on.