将生产的.o文件放进指定的文件中(先创建该文件夹)
src = $(wildcard ./*.cpp) obj = $(patsubst %.cpp,./output/%.o,$(src)) target = test $(target) : $(obj) g++ $(obj) -o $(target) %.o: %.cpp g++ -c $< -o output/$@ .PHONY:clean clean: rm -f $(target) $(obj)
将生产的最终可执行文件放进指定的文件夹中(可以不先创建文件夹)
VERSION = 1.0.0 SOURCE = $(wildcard ./*.cpp) OBJ = $(patsubst %.cpp,%.o,$(SOURCE)) INCLUDE = -I /usr/include/mysql/ LIBS = -lmysqlclient LIB_PATH = -L /usr/lib/mysql/ CFALGS = -g TARGET = test $(TARGET): $(OBJ) @mkdir -p output/ g++ $(OBJ) $(LIB_PATH) $(LIBS) -o output/$(TARGET).$(VERSION) %.o : %.cpp g++ $(INCLUDE) $(CFALGS) -c $< -o $@ .PHONY: clean clean: rm -rf $(OBJ) output/