我有一个makefile,当我需要添加作为项目一部分的另一个文件时,我还需要大量编辑以使其全部工作。我只想编写一次我的makefile并完成它!所以,这是脚本:
CC=g++
CFLAGS=-c -Wall
#All paths start in the main directory which has this makefile, object/ and code/.
OBJPATH=object#All compiled .o files need to be placed here.
CODEPATH=code#All .cpp and .hpp header files reside here.
#The names of all the object files that need to be made.
OBJ=$(OBJPATH)/World.o $(OBJPATH)/Block.o $(OBJPATH)/Creep.o $(OBJPATH)/Food.o $(OBJPATH)/Brick.o
all : World.a
$(OBJPATH)/World.o : $(CODEPATH)/World.hpp $(CODEPATH)/World.cpp
$(CC) $(CFLAGS) $(CODEPATH)/World.cpp
mv World.o $(OBJPATH)/
$(OBJPATH)/Block.o : $(CODEPATH)/Block.hpp $(CODEPATH)/Block.cpp
$(CC) $(CFLAGS) $(CODEPATH)/Block.cpp
mv Block.o $(OBJPATH)/
$(OBJPATH)/Creep.o : $(CODEPATH)/Creep.hpp $(CODEPATH)/Creep.cpp
$(CC) $(CFLAGS) $(CODEPATH)/Creep.cpp
mv Creep.o $(OBJPATH)/
$(OBJPATH)/Food.o : $(CODEPATH)/Food.hpp $(CODEPATH)/Food.cpp
$(CC) $(CFLAGS) $(CODEPATH)/Food.cpp
mv Food.o $(OBJPATH)/
$(OBJPATH)/Brick.o : $(CODEPATH)/Brick.hpp $(CODEPATH)/Brick.cpp
$(CC) $(CFLAGS) $(CODEPATH)/Brick.cpp
mv Brick.o $(OBJPATH)/
#This archive is the final result of the compilation:
World.a : $(OBJ)
ar rvs World.a $(OBJ)
cp $(CODEPATH)/World.hpp . #I need both the World.a file and the World.hpp file from code/ for this library to function.
#Delete all object files and the archive:
clean :
rm $(OBJPATH)/*.o World.a World.hpp *~ $(CODEPATH)/*~
#Delete all backup files made by Gedit:
pretty :
rm *~ $(CODEPATH)/*~
这基本上是编译.o
文件夹中的所有文件object/
,然后将它们放入一个名为World.a
. 然后它只是复制过来World.hpp
,因为那是库的头文件。我相信很多这样的代码可以被最小化,因此它更具可读性和通用性(不需要我每次创建新类时都对其进行编辑)。