1. 程式人生 > >一個能夠自動匹配檔案並編譯的makefile

一個能夠自動匹配檔案並編譯的makefile

AllDirs := $(shell ls -R | grep '^\./.*:$$' | awk '{gsub(":","");print}') .
Sources := $(foreach n,$(AllDirs) , $(wildcard $(n)/*.cpp))
Objs := $(patsubst %.cpp,%.o, $(Sources))
Deps := $(patsubst %.cpp,%.d, $(Sources))
 
CC = gcc
CXX = g++
RM = rm -f
CXXFLAGS = -g -O2  -Wall
#CPPFLAGS = $(foreach n,$(AllDirs), -I$(n))
CPPFLAGS = -Iinclude
LDFLAGS = 
 
TARGET = test
 
all: $(TARGET)
 
$(TARGET): $(Objs)
	$(CXX) -g -o 
[email protected]
$^ $(LDFLAGS) %.d : %.c $(CC) -MT"$(<:.cpp=.o) [email protected]" -MM $(CXXFLAGS) $(CPPFLAGS) $< > [email protected] %.d : %.cpp $(CXX) -MT"$(<:.cpp=.o) [email protected]" -MM $(CXXFLAGS) $(CPPFLAGS) $< > [email protected] sinclude $(Deps) .PHONY : clean clean: $(RM) $(Objs) $(Deps) $(TARGET)