简体   繁体   English

需要帮助来了解makefile模式规则%:%.o

[英]Need help understanding makefile pattern rule % : %.o

I want to change this makefile into something simpler using pattern rules I read about in a book: 我想使用我在书中读到的模式规则将此makefile更改为更简单的内容:

VPATH = src
CPPFLAGS = -I include

main.o: main.cpp
    g++ $(CPPFLAGS) $<
TwoDimensionalShape.o: TwoDimensionalShape.cpp
    g++ -c $(CPPFLAGS) $<
Square.o: Square.cpp Square.h
    g++ -c $(CPPFLAGS) $<
Circle.o: Circle.cpp Circle.h
    g++ -c $(CPPFLAGS) $<
Rectangle.o: Rectangle.cpp Rectangle.h
    g++ -c $(CPPFLAGS) $<
Triangle.o: Triangle.cpp Triangle.h
    g++ -c $(CPPFLAGS) $<
ShapeStack.o: ShapeStack.cpp ShapeStack.h
    g++ -c $(CPPFLAGS) $<
ScreenManager.o: ScreenManager.cpp ScreenManager.h
    g++ -c $(CPPFLAGS) $<
ScreenState.o: ScreenState.cpp ScreenState.h
    g++ -c $(CPPFLAGS) $<
SquareState.o: SquareState.cpp SquareState.h
    g++ -c $(CPPFLAGS) $<
CircleState.o: CircleState.cpp CircleState.h
    g++ -c $(CPPFLAGS) $<

After reading the book I can write the above using pattern rules like this. 阅读本书后,我可以使用类似的模式规则编写以上内容。 But I don't understand how it is working: 但是我不明白它是如何工作的:

#source files are in "src" folder.
VPATH = src
#header files are in "include" folder.
CPPFLAGS = -I include -Wall

all: main.o TwoDimensionalShape.o Square.o Circle.o Rectangle.o Triangle.o ShapeStack.o ScreenManager.o ScreenState.o SquareState.o CircleState.o
    g++ $(CPPFLAGS) $^

%.o: %.cpp
    g++ -c $(CPPFLAGS) $<

%: %.o
    g++ $<

This makefile is correct however I don't understand how it is working. 这个makefile是正确的,但是我不知道它是如何工作的。

  1. If I change for example 2 source files, how does this makefile understand to only compile the changed two source files and not all of source files? 如果我更改了例如2个源文件,那么该makefile如何理解仅编译更改后的两个源文件而不编译所有源文件?
  2. In the book I read, example was about C not C++ and last line was %: %.c . 在我读的书中,示例是关于C而不是C ++的,最后一行是%: %.c . %: %.c Then why is my line which is %: %.o working? 那么为什么我的%: %.o有效? Shouldn't it be %: %.cpp ? 应该不是%: %.cpp吗?

This makefile is correct however I don't understand how it is working. 这个makefile是正确的,但是我不知道它是如何工作的。

If your new Makefile is a replacement for the old one, it is definitely NOT working. 如果新的Makefile替代了旧的Makefile,则肯定无法正常工作。

In the "old" one you have eg 在“旧”中,例如

ShapeStack.o: ShapeStack.cpp ShapeStack.h

which tells that ShapeStack.o depends on the .cpp and header file. 这表明ShapeStack.o取决于.cpp和头文件。 Your new Makefile did not have any dependencies to other files which will result in a lot of trouble. 您的新Makefile与其他文件没有任何依赖关系,这将导致很多麻烦。 Simply touch one of your headers and type make. 只需触摸您的标题之一并输入make。 Nothing will happen! 什么都不会发生!

So at minimum you have to introduce your source file dependecies, maybe manual as in your old makefile or with some more automatism which uses the dependency check from the compiler, using gcc it is with "gcc -MM". 因此,至少您必须引入源文件的依赖关系,也许像在旧的makefile中一样手动输入,或者具有更多的自动性,它会使用编译器的依赖关系检查,而gcc就是“ gcc -MM”。

For getting automated prerequisites see https://www.gnu.org/software/make/manual/html_node/Automatic-Prerequisites.html 有关获取自动化先决条件的信息,请参见https://www.gnu.org/software/make/manual/html_node/Automatic-Prerequisites.html

And using vpath can result in a lot of trouble. 使用vpath可能会导致很多麻烦。 There are some articels, eg: http://make.mad-scientist.net/papers/how-not-to-use-vpath/ 有一些文章,例如: http : //make.mad-scientist.net/papers/how-not-to-use-vpath/

Some example Makefiles can be found already here: minimum c++ make file for linux 可以在这里找到一些示例Makefile: Linux的最小c ++ make文件

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

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