C++ - 使用g ++创建Makefile

Big*_*ger 2 c++ makefile g++

我为我的CMSC 202课程项目'Blackjack'制作了一个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 at the end                 
EXEC = Proj2.out                                                      
# The flags to use for compilation                                    
FLAGS = -Wall                                                         
# The code compiler to use for compilation                            
CC = g++                                                              

# Perform action on all object files (May or may not exist)           
all: $(OBJECTS)                                                       
        $(CC) $(FLAGS) -o $(EXEC) $(OBJECTS)
Run Code Online (Sandbox Code Playgroud)

这是我make在终端呼叫时的终端输出.

g++    -c -o Proj2.o Proj2.cpp
g++    -c -o Blackjack.o Blackjack.cpp
g++    -c -o Deck.o Deck.cpp
g++    -c -o Card.o Card.cpp
g++    -c -o Hand.o Hand.cpp
g++    -c -o Player.o Player.cpp
g++ -Wall -o Proj2.out Proj2.o Blackjack.o Deck.o Card.o Hand.o Player.o
Run Code Online (Sandbox Code Playgroud)

谁能告诉我.o文件是如何编译的?它看起来不像是g++ -c -o $.o $.cpp在Makefile中的任何地方提示使用该命令进行编译.我也没有声明使用任何.cpp文件.

预先感谢您的帮助.

编辑

感谢您的大力帮助,这是我使用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
Run Code Online (Sandbox Code Playgroud)

非常感谢所有贡献的人.

tot*_*oth 5

Make有一组隐式规则(参见此处参考).例如

Compiling C++ programs
`n.o' is made automatically from `n.cc' or `n.C' with a command of the form 
`$(CXX) -c $(CPPFLAGS)   $(CXXFLAGS)'. 
Run Code Online (Sandbox Code Playgroud)

大多数make还会将此规则用于.cpp文件.当make看到你的某个目标有xo需求时,它将尝试查看它是否可以使用隐式规则生成xo,并且在你的情况下发现它可以从.cpp文件开始.