当前位置:网站首页>为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/
边栏推荐
- 12.udp protocol -bite
- 89. (cesium article) cesium aggregation diagram (custom picture)
- Flink SQL task taskmanager memory settings
- 关于SQL+NoSQL : NewSQL数据库
- 从第三次技术革命看企业应用三大开发趋势
- Introduction to radar antenna
- EasyGBS调用获取实时快照接口时,出现白色方块该如何解决?
- 面试官:说一下MySQL事务隔离级别?
- C language big job - Matching System
- radar transmitter
猜你喜欢

14.ip protocol -bite

C#学习二:堆和栈

PostgreSQL source code learning (23) -- transaction log ④ - log assembly

如何使用SMS向客户传递服务信息?指南在这里!

Leetcode-470- implement rand10() with rand7()

LeetCode-234-回文链表

12.UDP协议-bite

Digital tracking analysis of insurance services in the first quarter of 2022

动作捕捉系统用于苹果采摘机器人

2022年第一季度保险服务数字化跟踪分析
随机推荐
Applet judges that the data is not empty
火山引擎入选国内首个《边缘计算产业全景图》
Classe d'outils commune de fichier, application liée au flux (enregistrement)
When easygbs calls the interface for obtaining real-time snapshots, how to solve the problem of white squares?
面试官:说一下MySQL事务隔离级别?
Basic composition of radar
LeetCode-234-回文链表
Digital tracking analysis of insurance services in the first quarter of 2022
PostgreSQL source code learning (25) -- transaction log ⑥ - wait for log writing to complete
File常用工具类, 流相关运用 (记录)
Taro中添加小程序 “lazyCodeLoading“: “requiredComponents“,
13.TCP-bite
Taro 小程序开启wxml代码压缩
To prevent enterprise data leakage, use the fortress machine of network security products!
Symfony framework security component firewall configuration
微信公告号 图灵机器人实现智能回复
Pre war minesweeping: five measures for vulnerability management
three.js和高德地图结合引入obj格式模型-效果演示
el-table-column行按钮防重控制loading
华为云AOM 2.0版本发布