BUILD_DIR ?= build
$(BUILD_DIR)/bin/%:
@echo "Building $@"
@mkdir -p $(@D)
GOBIN=$(abspath $(@D)) go install -tags "$(GO_TAGS)" -ldflags "$(GO_LDFLAGS)" $(pkgmap.$(@F))
@touch $@
?=
?= 是如果没有被赋值过就赋予等号后面的值
BUILD_DIR ?= build
$()
引用变量
$(BUILD_DIR)
%
匹配符号,如例:匹配所有以$(BUILD_DIR)/bin为前缀的
$(BUILD_DIR)/bin/%:
:
例
install : editor
mv editor /usr/local
执行install时,如果editor有更新,那么执行tab的下一行,即mv editor /usr/local。执行该操作后,不会得到名为install的文件,因此也称作伪target。(非伪target见下text.o)
text.o : text.c com.h
gcc -c text.c
$@
$@指代当前目标,就是Make命令当前构建的那个目标。比如,make foo的 $@ 就指代foo。
$(BUILD_DIR)/bin/%:
@echo "Building $@"
$(@D)
表示$@的目录部分(不以斜杠作为结尾) ,如果$@值是"dir/foo.o",那么$(@D)就是"dir",而如果$@中没有包含斜杠的话,其值就是"."(当前目录)
BUILD_DIR ?= build
$(BUILD_DIR)/bin/%:
@echo "Building $@"
@mkdir -p $(@D)
GOBIN=$(abspath $(@D)) go install -tags "$(GO_TAGS)" -ldflags "$(GO_LDFLAGS)" $(pkgmap.$(@F))
@touch $@
$(@F)
表示"$@“的文件部分,如果”$@“值是"dir/foo.o”,那么"$(@F)“就是"foo.o”,"$(@F)“相当于函数”$(notdir $@)"
GOBIN=$(abspath $(@D)) go install -tags "$(GO_TAGS)" -ldflags "$(GO_LDFLAGS)" $(pkgmap.$(@F))
abspath
$(abspath _names)
该函数主要用于将_names中的各路径转换成绝对路径,并将转换后的结果返回。
GOBIN=$(abspath $(@D)) go install -tags "$(GO_TAGS)" -ldflags "$(GO_LDFLAGS)" $(pkgmap.$(@F))
@F
自动化变量$(@F)
The file-within-directory part of the file name of
the target. If the value of ‘@’isdir/foo.othen‘@’ is dir/foo.o then ‘@’isdir/foo.othen‘(@F)’ is foo.o.
‘(@F)’isequivalentto‘(@F)’ is equivalent to ‘(@F)’isequivalentto‘(notdir $@)’.
想用非makefile文件名编译makefile文件形式时
想要用makefile编译makefile3.mak的时候,就:
make -f makefil3.mak
冒号代表了什么
<target> : <prerequisites>
[tab] <commands>
上面第一行冒号前面的部分,叫做"目标"(target),冒号后面的部分叫做"前置条件"(prerequisites);第二行必须由一个tab键起首,后面跟着"命令"(commands)。
$*
所有参数列表。如"$*“用「”」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
本文详细介绍了Makefile中的关键语法元素,包括变量赋值、目标与依赖、自动化变量的使用,以及如何通过Makefile组织和编译Go语言项目。通过对`BUILD_DIR`、`%`、`$@`、`$(@D)`和`$(@F)`等概念的解释,揭示了Makefile在构建过程中的作用和工作原理。
1万+

被折叠的 条评论
为什么被折叠?



