[英]MakeFile in g++ . Ignorable commands
我正在學習如何在g ++中制作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
現在,我對此有一些疑問,為了方便查詢,我還使用statement關鍵字引用了特定的行。
1-我的理解對語句1是正確的。當g ++在main之后遇到main.o(這是對目標main.o的依賴項)時:它跳到目標main.0(語句2)。 然后檢查main.o的依賴關系(如果找到了兩個頭文件),然后運行命令,然后返回以完成下一個依賴關系的任務,依此類推?
2-對於Point.h的Point.o的依賴性,為什么沒有這樣的命令
$(CXX) $(CXXFLAGS) -c Point.cpp
對於Rectangle.o同樣如此,為什么沒有這樣的依賴關系命令
$(CXX)$(CXXFLAGS)-c Rectangle.cpp
如果有人可以澄清這一點,我將不勝感激。
正確。 值得注意的是,gnu make
使用修改時間來確定是否滿足依賴性。
make
具有用於構建.o
目標的隱式規則 。 這些使用變量,例如$CXX
和$CXXFLAGS
。 對於給定的目標,例如foo.o
, make
將根據源文件foo.<ext>
的存在情況應用規則,從而創建目標依賴對foo.o : foo.<ext>
。 如果擴展名是C ++擴展名(例如.cpp
, .cc
, .C
),則它將沿
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c
您已經為Point.o
指定了額外的依賴關系 。 這將添加到隱式依賴項Point.cpp
。 如果任何的修改時間Point.h
或Point.cpp
比的新Point.o
,該規則將被運行。
正如@Basile在注釋中指出的那樣,您可以使用-p
或--print-data-base
選項調用make
以獲得有關make的“狀態”的信息,包括隱式規則。 例如,將它與grep結合使用,我可以查詢從C ++文件構建.o
文件的隱式規則:
make -p | grep -A 1 -B 1 COMPILE.cc
輸出:
# 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) $<
關於您擴展的規則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.