C++ - 使用 g++ 的 Makefile
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22558709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ - Makefile using g++
提问by BigBerger
I have made a Makefile for my CMSC 202 course project, 'BlackHyman'. It does everything I need it to and it works perfectly. You may be asking why I posted here then, this is because I have no idea how it works and I didn't use any other resources but myself to create it.
我为我的 CMSC 202 课程项目“BlackHyman”制作了一个 Makefile。它可以完成我需要的一切,而且效果很好。你可能会问为什么我当时在这里发帖,这是因为我不知道它是如何工作的,而且我没有使用任何其他资源,而是我自己来创建它。
Here is my Makefile code.
这是我的 Makefile 代码。
# Object files to either reference or create
OBJECTS = Proj2.o BlackHyman.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)
Here is the terminal output when I call make
in the terminal.
这是我make
在终端中调用时的终端输出。
g++ -c -o Proj2.o Proj2.cpp
g++ -c -o BlackHyman.o BlackHyman.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 BlackHyman.o Deck.o Card.o Hand.o Player.o
Can anyone tell me how the .o files are being compiled? It does not look like they are being prompted to be compiled with that g++ -c -o $.o $.cpp
command anywhere in the Makefile. Nor did I state to use any .cpp files.
谁能告诉我 .o 文件是如何编译的?看起来他们不会被提示g++ -c -o $.o $.cpp
在 Makefile 的任何地方使用该命令进行编译。我也没有声明使用任何 .cpp 文件。
Thank you in advance for your help.
预先感谢您的帮助。
Edit
编辑
Thanks to all your great help, this is now the terminal output I receive when using make.
感谢您的大力帮助,现在这是我在使用 make 时收到的终端输出。
g++ -Wall -c -o Proj2.o Proj2.cpp
g++ -Wall -c -o BlackHyman.o BlackHyman.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 BlackHyman.o Deck.o Card.o Hand.o Player.o
Thank you so much to all of you who have contributed.
非常感谢所有做出贡献的人。
回答by toth
Make has a set of implicit rules (see herefor a reference). For instance
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)'.
Most make's will also use this rule for .cpp files. When make sees there's a x.o requirement for one of your targets, it will try to see if it can generate x.o using implicit rules, and in your case find it can do it starting from a .cpp file.
大多数 make 也会将此规则用于 .cpp 文件。当 make 看到您的目标之一有 xo 要求时,它会尝试查看它是否可以使用隐式规则生成 xo,并且在您的情况下,它可以从 .cpp 文件开始执行。
回答by Benjamin Bannier
This Makefile uses implicit ruleswhich are a great way to reduce duplication.
这个 Makefile 使用隐式规则,这是减少重复的好方法。
By default the first target will be built, here all
. It depends on a number
of object files listed in a variable $OBJECTS
, e.g. Proj2.o
who's
dependencies aren't listed in the Makefile. Now if make sees an input file in the current directory
with a matching name, e.g. Proj2.cpp
it will try
to build Proj2.o
from it (there are other implicit rules for sources in
other languages). Proj2.o
would then be built by default with the command
默认情况下,将在此处构建第一个目标all
。它取决于变量中列出的许多目标文件$OBJECTS
,例如Proj2.o
,Makefile 中未列出谁的依赖项。现在,如果 make 在当前目录中看到一个具有匹配名称的输入文件,例如Proj2.cpp
,它将尝试从中构建Proj2.o
(对于其他语言的源,还有其他隐式规则)。Proj2.o
然后将默认使用命令构建
$(CXX) $(CXXFLAGS) -c -o Proj2.o
where $(CXX)
the name of the C++ compiler (g++
in your case).
其中$(CXX)
C++ 编译器的名称(g++
在您的情况下)。
The explicit build step for all
assembles all the object files into the
target executable.
显式构建步骤将all
所有目标文件组装到目标可执行文件中。
Looking at above build command you'll notice a small problem in your Makefile. Since the flags to the C++ compiler are given in a variable FLAGS
and not the standard CXXFLAGS
no warnings will be emitted when building the object files. Using the standard name would fix this (you do want warnings, maybe even more than -Wall
gives you).
查看上面的构建命令,您会注意到 Makefile 中有一个小问题。由于 C++ 编译器的标志是在变量中给出的,FLAGS
而不是标准的,CXXFLAGS
因此在构建目标文件时不会发出警告。使用标准名称可以解决这个问题(你确实需要警告,甚至可能比-Wall
给你的更多)。