Here is my project:
project
|--- main.cpp
|--- makefile
|--- test
|--- Test.cpp
|--- Test.h
Here is the makefile
:
g++1x:=g++ -std=c++14 -stdlib=libc++ -MMD -MP
cflags:= -Wall -lncurses
PATHS:=./ ./test/
TARGET:=matrix.out
SRC:=$(foreach PATH,$(PATHS),$(wildcard $(PATH)/*.cpp))
OBJDIR:=.obj
OBJ:=$(addprefix $(OBJDIR)/,$(notdir $(SRC:.cpp=.o)))
.PHONY: install
install: $(OBJDIR) $(TARGET)
$(OBJDIR):
mkdir -p $(OBJDIR)
$(TARGET): $(OBJ)
$(g++1x) $(cflags) -o $@ $^ -g
$(OBJDIR)/%.o: %.cpp
$(g++1x) -c -o $@ $< -g
$(OBJDIR)/%.o: ./test/%.cpp
$(g++1x) -c -o $@ $< -g
-include $(addprefix $(OBJDIR)/,$(notdir $(SRC:.cpp=.d)))
.PHONY: clean
clean:
rm -f $(TARGET)
rm -rf $(OBJDIR)
It works well but I have two questions:
1) Is it possible to avoid foreach
for PATHS
so that I can use the same makefile
for all of cpp projects?
2) As you see, to generate main.o
and Test.o
I write two blocks:
$(OBJDIR)/%.o: ./test/%.cpp
and $(OBJDIR)/%.o: %.cpp
.
Is it possible to write only once?
I've tried as below but it doesn't work:
$(OBJDIR)/%.o: $(foreach PATH,$(PATHS),$(wildcard $(PATH)/%.cpp))
$(g++1x) -c -o $@ $< -g
I've even tried like this but it doesn't work still:
$(OBJDIR)/%.o: %.cpp ./test/%.cpp
$(g++1x) -c -o $@ $< -g
makefile
template.