当前位置:网站首页>为Golang项目编写Makefile
为Golang项目编写Makefile
2022-06-29 15:36:00 【学亮编程手记】
为Golang项目编写Makefile
| Golang
借助Makefile我们在编译过程中不再需要每次手动输入编译的命令和编译的参数,可以极大简化项目编译过程。
make介绍
make是一个构建自动化工具,会在当前目录下寻找Makefile或makefile文件。如果存在相应的文件,它就会依据其中定义好的规则完成构建任务。
Makefile介绍
我们可以把Makefile简单理解为它定义了一个项目文件的编译规则。借助Makefile我们在编译过程中不再需要每次手动输入编译的命令和编译的参数,可以极大简化项目编译过程。同时使用Makefile也可以在项目中确定具体的编译规则和流程,很多开源项目中都会定义Makefile文件。
本文不会详细介绍Makefile的各种规则,只会给出Go项目中常用的Makefile示例。关于Makefile的详细内容推荐阅读Makefile教程。
规则概述
Makefile由多条规则组成,每条规则主要由两个部分组成,分别是依赖的关系和执行的命令。
其结构如下所示:
[target] ... : [prerequisites] ...
<tab>[command]
...
...
其中:
- targets:规则的目标
- prerequisites:可选的要生成 targets 需要的文件或者是目标。
- command:make 需要执行的命令(任意的 shell 命令)。可以有多条命令,每一条命令占一行。
举个例子:
build:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o xx
示例
.PHONY: all build run gotool clean help
BINARY="bluebell"
all: gotool build
build:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${BINARY}
run:
@go run ./
gotool:
go fmt ./
go vet ./
clean:
@if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi
help:
@echo "make - 格式化 Go 代码, 并编译生成二进制文件"
@echo "make build - 编译 Go 代码, 生成二进制文件"
@echo "make run - 直接运行 Go 代码"
@echo "make clean - 移除二进制文件和 vim swap files"
@echo "make gotool - 运行 Go 工具 'fmt' and 'vet'"
其中:
BINARY="bluebell"是定义变量。.PHONY用来定义伪目标。不创建目标文件,而是去执行这个目标下面的命令。
参考链接:https://www.liwenzhou.com/posts/Go/makefile/
边栏推荐
- el-table-column行按钮防重控制loading
- Three development trends of enterprise application viewed from the third technological revolution
- The way of enterprise transformation and upgrading: digital transformation, thinking first
- PWM to 0-5v/0-10v/1-5v linear signal transmitter
- kotlin 注解聲明與使用
- 火山引擎入选国内首个《边缘计算产业全景图》
- STM32与GD32笔记
- What are the advantages of intelligent chat robots? Senior independent station sellers tell you!
- 微信公共号开发,发送消息回复文本
- LeetCode-470-用Rand7()实现Rand10()
猜你喜欢

What are the advantages of intelligent chat robots? Senior independent station sellers tell you!

《网络是怎么样连接的》读书笔记 - 服务器端的局域网中(四)

Volcano engine was selected into the first "panorama of edge computing industry" in China

LeetCode-470-用Rand7()实现Rand10()

商业智能BI与业务管理决策思维之三:业务质量分析

13.TCP-bite

cmake学习-2

硬件开发笔记(八): 硬件开发基本流程,制作一个USB转RS232的模块(七):创建基础DIP元器件(晶振)封装并关联原理图元器件
[data analysis] five common questions about learning SQL?

File常用工具类, 流相关运用 (记录)
随机推荐
PostgreSQL source code learning (23) -- transaction log ④ - log assembly
Leetcode-64- minimum path sum
Several imaging modes of polarimetric SAR
Houdini图文笔记:VAT(3.0)导入UE4/5的设置向导[官方文档翻译]
Leetcode notes: biweekly contest 81
Leetcode-470- implement rand10() with rand7()
发明了杀毒软件之后,他选择做一个极品混混
GWD: rotating target detection based on Gaussian Wasserstein distance | ICML 2021
C # learning 1: value type and reference type
Is there any lack of dependence? An error is reported when flinksql is packaged and running, but there is no problem when the local idea runs. Solve it. Thanks
89. (cesium article) cesium aggregation diagram (custom picture)
我想知道我在南宁,到哪里开户比较好?另外,手机开户安全么?
File常用工具类, 流相关运用 (记录)
动作捕捉系统用于苹果采摘机器人
Volcano engine was selected into the first "panorama of edge computing industry" in China
Paging SQL (rownum, row_number, deny_rank, rank)
Pre war minesweeping: five measures for vulnerability management
商业智能BI与业务管理决策思维之三:业务质量分析
Scroll, do you understand?
kotlin 注解声明与使用