我有一个Makefile,它适用于我如何使用它,但是谁能告诉我我所做的是不是一个好的实践?或者是否有更好、更干净或更有效的方法来实现我要达到的目标?
这是我的Makefile代码。
# Object files to either reference or create
OBJECTS = Proj2.o Blackjack.o Deck.o Card.o Hand.o Player.o
# The executable file that will be created
EXEC = Proj2.out
# The c++ flags to use for compilation
CXXFLAGS = -Wall
# The c++ compiler to use for compilation
CXX = g++
# This section is called on 'make'
# Will call compile, and then call clean
all: compile clean
# Perform action on all object files (May or may not exist)
# The makefile will implicitly compile all .o files needed
# Will also compile them into the EXEC file listed
compile: $(OBJECTS)
$(CXX) $(CXXFLAGS) -o $(EXEC) $(OBJECTS)
# This section is called after compilation is completed
# This will clean all existing .o files listed in the directory
clean:
rm -f *.o下面是我调用make时的终端输出。
g++ -Wall -c -o Proj2.o Proj2.cpp
g++ -Wall -c -o Blackjack.o Blackjack.cpp
g++ -Wall -c -o Deck.o Deck.cpp
g++ -Wall -c -o Card.o Card.cpp
g++ -Wall -c -o Hand.o Hand.cpp
g++ -Wall -c -o Player.o Player.cpp
g++ -Wall -o Proj2.out Proj2.o Blackjack.o Deck.o Card.o Hand.o Player.o
rm -f *.o使用像这样的Makefile是一种好的做法吗?具体地说,我是否正确地完成了Makefile的清理部分?
发布于 2014-03-22 06:07:03
您根本不应该让all依赖于clean。通过这样做,您可以确保每次运行make时都必须重新编译所有内容。如果您想这样做,那么使用make本身是无用的:只需编写一个shell脚本来编译和链接您的代码。
clean目标应该是一个单独的目标,如果您想要清理您的工作区,您可以显式地运行make clean。
makefile的另一个问题是,链接规则将compile作为目标列出,但它会构建$(EXE)。让规则创建一个文件,而这个文件并不完全是您告诉make它将构建的目标,这几乎不是一个好主意。要确保这一点,请始终使用$@作为生成的目标。重写如下:
compile: $(EXE)
$(EXE): $(OBJECTS)
$(CXX) $(CXXFLAGS) -o $@ $^https://stackoverflow.com/questions/22570320
复制相似问题