有人可以解释这个make文件吗? [英] Could someone explain this make file?

查看:56
本文介绍了有人可以解释这个make文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在站点上找到了这个Makefile.他们没有解释这个例子,所以我想知道是否有人正在发生什么.

I found this makefile on this site. They don't explain this example, so I was wondering if anybody new what was going on.

CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=main.cpp hello.cpp factorial.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=hello

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) 
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $< -o $@

推荐答案

CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=main.cpp hello.cpp factorial.cpp

将四个变量设置为常量字符串.对于makefile的其余部分,例如出现$(CC)的位置,它将被替换为g++

sets four variables to be constant strings. For the rest of the makefile, wherever $(CC) appears (for example), it will be replaced by g++

OBJECTS=$(SOURCES:.cpp=.o)

将变量OBJECTS设置为与SOURCES相同,除了模式.cpp在SOURCES的单词中出现的地方,其替换为.o

sets the variable OBJECTS to be the same as SOURCES, except wherever the pattern .cpp appears in a words of SOURCES, its replaced by .o

EXECUTABLE=hello

设置另一个常量字符串var

sets another constant string var

all: $(SOURCES) $(EXECUTABLE)

makefile中的第一个实际规则,它告诉make要构建all,它必须首先构建$(SOURCES)$(EXECUTABLE)中的所有内容,然后不执行任何操作.由于这是第一个,因此它成为默认目标,因此运行make等效于make all

The first actual rule in the makefile, This tells make that to build all it must first build everything in $(SOURCES) and $(EXECUTABLE), and then do nothing. Since this is first, it becomes the default target, so running make is equivalent to make all

$(EXECUTABLE): $(OBJECTS) 
        $(CC) $(LDFLAGS) $(OBJECTS) -o $@

另一条规则:要创建$(EXECUTABLE)(扩展为hello),它必须首先在$(OBJECTS)(等同于main.o hello.o factorial.o)中构建所有内容,然后运行命令$(CC) $(LDFLAGS) $(OBJECTS) -o $@

Another rule: to create $(EXECUTABLE) (which expands to hello) it must first build everything in $(OBJECTS) (equivalent to main.o hello.o factorial.o) and then run the command $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
        $(CC) $(CFLAGS) -o $@ $<

模式规则:为了构建以.o结尾的文件,请首先重建/创建/查找以.cpp结尾的相应文件,然后运行命令$(CC) $(CFLAGS) -o $@ $<.

A pattern rule: in order to build a file ending in .o, first rebuild/create/find the corresponding file ending in .cpp, and then run the command $(CC) $(CFLAGS) -o $@ $<.

这最后两个规则包含特殊变量$@$<,它们仅在规则操作中有效,并分别扩展到目标和第一依赖项

These last two rules contain the special variables $@ and $< which are only valid in rule actions and expand to the target and first dependency respectively

因此,当您运行make时,它将读取所有内容,然后尝试构建默认目标(全部). 由于它不存在,因此它将尝试构建文件main.cpp,hello.cpp,factorial.cpp和hello.由于前三个(大概)存在,因此它会为它们查找规则/依赖关系,但找不到任何规则/依赖关系,因此决定对它们无事可做.如果它们不存在,make会给出一个错误,提示没有规则可以使目标'main.cpp'"

So when you run make, it reads all this and then tries to build the default target (all). Since it doesn't exist, it tries to build the files main.cpp, hello.cpp, factorial.cpp, and hello. Since the first 3 (presumably) exist, it looks for rules/dependencies for them, but doesn't find any, so decides there's nothing to do for them. If they didn't exist, make would give an error saying "no rule to make target 'main.cpp'"

在"hello"的情况下,它取决于main.o,hello.o和factorial.o,因此它会调查它们.对于main.o,模式规则说它取决于main.cpp,因此,如果main.o不存在或main.cpp是较新版本,它将运行命令g++ -c -Wall -o main.o main.cpp. hello.o和factorial.o也会发生同样的情况.

In the case of "hello" it depends on main.o, hello.o and factorial.o, so it looks into them. For main.o, the pattern rule says it depends on main.cpp, so if main.o doesn't exist or if main.cpp is newer, it will run the command g++ -c -Wall -o main.o main.cpp. The same happens for hello.o and factorial.o.

完成这些操作后,如果hello不存在或早于任何.o文件(可能刚刚更改,因此可能很新),它将运行该命令来重新链接它.最后,它将运行空命令(不执行任何操作)以全部"重建.

Once those are done, if hello doesn't exist or is older than any of those .o files (which may have just changed, so are possibly pretty new), it will run that command to relink it. Finally, it will run the empty command (doing nothing) to 'rebuild' all.

这篇关于有人可以解释这个make文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆