在很多C/C++工程中源文件放到一个目录下面,头文件放在另一个目录下面,这样生成的目标文件(.o文件)直接放在源文件相同的文件夹下。但是对于多个目录中都存在源文件的情况下,无论将目标文件放在一个目录下还是放在和对应源文件相同的目录下都不像所有源文件在一个目录下那样简单。下面取makefile中的一部分来说明,该makefile是将目标文件放在和对应的源文件相同的目录下。
CXXFLAGS := -std=c++11 -g -Wall
CXX := g++SRC1 = $(wildcard ./*.cpp)
SRC2 = $(wildcard ./table/*.cpp)
SRC3 = $(wildcard ./checker/*.cpp)
OBJ1 = $(patsubst %.cpp,%.o,$(SRC1))
OBJ2 = $(patsubst %.cpp,%.o,$(SRC2))
OBJ3 = $(patsubst %.cpp,%.o,$(SRC3))
src2 = ./table/
src3 = ./checker/
TARGETS := main
$(TARGETS) : $(OBJ1) $(OBJ2) $(OBJ3)
@$(CXX) $^ $(LIB) -o $@
@echo "prepare successfully!"
%.o:$(src2)/%.cpp
@echo "prepare Compile "
@$(CXX) -c $< $(CXXFLAGS) $(INCLUDE) -o $@
@echo "Compiled "$<" successfully!"
%.o:$(src3)/%.cpp
@echo "prepare Compile "
@$(CXX) -c $< $(CXXFLAGS) $(INCLUDE) -o $@
@echo "Compiled "$<" successfully!"
%.o:%.cpp
@echo "prepare Compile "
@$(CXX) -c $< $(CXXFLAGS) $(INCLUDE) -o $@
@echo "Compiled "$<" successfully!"
.PHONY : clean
clean:
@-rm $(TARGETS) $(OBJ1) $(OBJ2) $(OBJ3) -f
@echo "Cleanup complete!"