简体   繁体   English

g ++中的MakeFile。 无知的命令

[英]MakeFile in g++ . Ignorable commands

I am learning how to make Makefile in g++. 我正在学习如何在g ++中制作Makefile I am using the following example The codes for the project is here .This is the Makefile 我使用以下示例该项目的代码在这里 。这是Makefile

# Makefile for Writing Make Files Example

# *****************************************************
# Variables to control Makefile operation

CXX = g++
CXXFLAGS = -Wall -g

# ****************************************************
# Targets needed to bring the executable up to date

main: main.o Point.o Rectangle.o  # ***Statement : 1 
    $(CXX) $(CXXFLAGS) -o main main.o Point.o Rectangle.o  #Commands underneath dependencies have tabs before them

# The main.o target can be written more simply

main.o: Point.h Rectangle.h           # ***Statement : 2
    $(CXX) $(CXXFLAGS) -c main.cpp

Point.o: Point.h                      # ***Statement : 3 


Rectangle.o: Rectangle.h Point.h      # ***Statement : 4

Now I have some questions regarding this I have also referenced specific lines using the statement keyword for ease of questioning. 现在,我对此有一些疑问,为了方便查询,我还使用statement关键字引用了特定的行。

1- Is my understanding correct of statement 1. When g++ encounters main.o (which is a dependency for target main.o) after main: it jumps to target main.0 (statement 2) . 1-我的理解对语句1是正确的。当g ++在main之后遇到main.o(这是对目标main.o的依赖项)时:它跳到目标main.0(语句2)。 It then checks the dependency of main.o if the dependency (the two header files are found) it then runs the command.then it comes back up to complete its task for the next dependency and so on ?? 然后检查main.o的依赖关系(如果找到了两个头文件),然后运行命令,然后返回以完成下一个依赖关系的任务,依此类推?

2-For dependency of Point.o which is Point.h why is there no command as such 2-对于Point.h的Point.o的依赖性,为什么没有这样的命令

$(CXX) $(CXXFLAGS) -c Point.cpp

and similarly for Rectangle.o why is there no command for its dependency as such 对于Rectangle.o同样如此,为什么没有这样的依赖关系命令

$(CXX) $(CXXFLAGS) -c Rectangle.cpp $(CXX)$(CXXFLAGS)-c Rectangle.cpp

I would appreciate it if someone could clarify this. 如果有人可以澄清这一点,我将不胜感激。

  1. Correct. 正确。 It is worth noting that gnu make uses modification time to determine whether a dependencies are met. 值得注意的是,gnu make使用修改时间来确定是否满足依赖性。

  2. make has implicit rules for building .o targets. make具有用于构建.o目标的隐式规则 These use variables such as $CXX and $CXXFLAGS . 这些使用变量,例如$CXX$CXXFLAGS For a given target, say foo.o , make will apply a rule depending on the presence of a source file foo.<ext> , creating a target-dependency pair foo.o : foo.<ext> . 对于给定的目标,例如foo.omake将根据源文件foo.<ext>的存在情况应用规则,从而创建目标依赖对foo.o : foo.<ext> If the extension is a C++ one (eg .cpp , .cc , .C ), then it will apply a rule along the lines of 如果扩展名是C ++扩展名(例如.cpp.cc.C ),则它将沿

    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c

You have specified extra dependency for Point.o . 您已经为Point.o指定了额外的依赖关系 This is added to the implicit dependency Point.cpp . 这将添加到隐式依赖项Point.cpp If the modification time of either Point.h or Point.cpp is newer than that of Point.o , the rule will be run. 如果任何的修改时间Point.hPoint.cpp比的新Point.o ,该规则将被运行。

As @Basile has pointed out in comments, you can invoke make with the -p or --print-data-base options to get information on the "state" of make, including the implicit rules. 正如@Basile在注释中指出的那样,您可以使用-p--print-data-base选项调用make以获得有关make的“状态”的信息,包括隐式规则。 Using it with grep, for example, I can query the implicit rules for building .o files from C++ files: 例如,将它与grep结合使用,我可以查询从C ++文件构建.o文件的隐式规则:

make -p | grep -A 1 -B 1 COMPILE.cc

Output: 输出:

# default
COMPILE.cpp = $(COMPILE.cc)
# makefile (from `Makefile', line 1)
--
--
# default
COMPILE.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
# environment
--
--
# default
COMPILE.C = $(COMPILE.cc)
# environment
--
--
#  commands to execute (built-in):
        $(COMPILE.cc) $(OUTPUT_OPTION) $<

--
--
#  commands to execute (built-in):
        $(COMPILE.cc) $(OUTPUT_OPTION) $<

Concerning the rule you extended, Point.o : Point.h , this is what my version of make would produce: 关于您扩展的规则Point.o : Point.h ,这是我的make版本会产生的结果:

make -p | grep -A 1 -B 1 Point
....
Point.o: Point.cpp Point.h
#  Implicit rule search has been done.
....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM