Makefile使用

基本使用

makefile使用,比如,当前有如下Golang代码需要编译 

package main

import "fmt"

func main(){
    fmt.Println("hello makefile!\n")
}

make支持makefile和Makefile两种命名方式,一般将文件命名为Makefile,以下内容分别表示为:

  • hello:  目标文件
  • main.go: 依赖文件
  • go build -o hello: 生成目标文件的命令

% cat Makefile

hello: main.go
	go build -o hello

直接输入make命令,会输出一行命令

% make     

go build -o hello

在当前目录可查看到编译后的hello文件已生成

% ./hello 

hello makefile!

清除编译后的文件

% cat Makefile

hello: main.go
	go build -o hello

clean:
	rm -f hello

执行make clean,会将hello二进制删除

% make clean

rm -f hello

如果不想输出执行的命令,比如rm -f hello,可以在命令前加@,一般都加上

% cat Makefile

hello: main.go
	@go build -o hello

.PHONY: clean
clean:
	@rm -f hello

伪目标

如上面的clean,不会生成目标文件,通常需要显式的声明一个目标为伪目标,用  .PHONY表示

% cat Makefile

hello: main.go
	go build -o hello

.PHONY: clean
clean:
	rm -f hello

执行make clean

% make clean

变量

# 变量
GO=go
.PHONY: build
build: 
	@$(GO) build -o hello

# = 赋值的是最终的值
IMAGE = alpine:1.0
A = a
B = $(A)b
A = c

# := 赋值的是当前值
D = d
E := $(D)e
D = f

# ?= 表示如果变量没被赋值,则赋予等号后的值
F ?= f
G = 3
G ?= g


# += 表示将等号后面的值添加到前面的变量上
h = 3
i = 4
i += $h
j = 5
k = $j$h

# 多行变量
define USAGE_OPTIONS

echo foo
echo bar
endef

# 环境变量
# 自定义的环境变量可以覆盖 Makefile 预定义的环境变量。默认情况下,Makefile 中定义的环境变量只在当前 Makefile 有效,如果想向下层传递(Makefile 中调用另一个 Makefile),需要使用 export 关键字来声明
export USAGE_OPTIONS_ENV = 10

# 特殊变量,make默认定义好的,可以直接使用的
# MAKE MAKECMDGOALS ...

# 自动化变量
# $@  $%  $<  $?  $^ $+ $| $* 


.PHONY: print-vars
print-vars:
	@echo $B     # cb
	@echo $E     # de
	@echo $F     # f
	@echo $G     # 3
	@echo $i     # 4 3  中间会有空格
	@echo $k     # 53  中间没有空格
	@echo $(USAGE_OPTIONS)  # foo\nbar
	@echo $(USAGE_OPTIONS_ENV) # 10

	@echo "特殊变量"
	@echo $(MAKE)
	@echo $(MAKECMDGOALS)

	@echo "自动变量"
	@echo $@ $% $< $? $^ $+ $| $*

特殊变量,可直接使用

 自动化变量

 条件判断

# 条件判断
l = 1
m = 2

ifeq ($(l),$(m))
	n = 10
else
	n = 20
endif

# ifneq 判断不等

# ifdef 判读是否已定义
ifdef m
	m = 1000
else
	m = 2000
endif

# ifndef 判断是否未定义

.PHONY: condition
condition:
	@echo $n
	@echo $m

函数

# 函数,本质上市定义一个多行变量,可以在 call 的作用下当作函数来使用,
在其他位置使用只能作为多行变量来使用

define Foo
    @echo "my name is $(0)"
    @echo "param is $(1)"
endef

var := $(call Foo)
new := $(Foo)

# 预定义函数
# subst ...
# 不用用"",比如str = "kongge_to"
# str = "kongge_to"

str = kongge_to

n-str1 := $(word 2, $(subst _, ,$(str))) 
n-str2 := $(word 2, $(subst _, ,"aaa_bbb_ccc")) 
PLATFORM = linux_amd64
GOOS := $(word 1, $(subst _, ,$(PLATFORM)))

.PHONY: func
func:
	@$(var) 1 2
	@$(new) 3 4
	@echo $(n-str1)
	@echo $(n-str2)
	@echo $(GOOS)

预定义函数

引入其他Makefile

# 引入其他makefile
include other/rule1/Makefile
include other/rule2/Makefile

.PHONY: other
other:
	@echo $(rule1)
	@echo $(rule2)

  % cat other/rule1/Makefile 

rule1 = this is rule1


.PHONY: rule1-1
rule1-1:
        @echo "rule1-1"

 % cat other/rule2/Makefile  

rule2 = this is rule2

可直接make rule1-1

% make rule1-1

更多

特别放送 | 给你一份Go项目中最常用的Makefile核心语法-极客时间

跟我一起写Makefile — 跟我一起写Makefile 1.0 文档

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值