Here is my project:
project
|
|
|
|
|
Here 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)
This works well, but I have two questions:
1) Can I escape foreachfor PATHSso that I can use the same makefilefor all cpp projects?
2) As you can see, for generation main.o, Test.oI also write two blocks:
$(OBJDIR)/%.o: ./test/%.cppand $(OBJDIR)/%.o: %.cpp.
Is it possible to write only once?
I tried as below, but this does not work:
$(OBJDIR)/%.o: $(foreach PATH,$(PATHS),$(wildcard $(PATH)/%.cpp))
$(g++1x) -c -o $@ $< -g
I even tried this, but it does not work:
$(OBJDIR)/%.o: %.cpp ./test/%.cpp
$(g++1x) -c -o $@ $< -g
source
share