当前位置:网站首页>为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/
边栏推荐
- Huawei cloud AOM version 2.0 release
- wallys/m.2/Adapter card(one pcie1x to 4 x Mini PCIE)
- 防范企业数据泄露,就用网络安全产品堡垒机!
- File常用工具类, 流相关运用 (记录)
- 89.(cesium篇)cesium聚合图(自定义图片)
- Numpy 的研究仿制 1
- PWM to 0-5v/0-10v/1-5v linear signal transmitter
- swift JSONSerialization
- 微信公告号自动回复使用图灵机器人实现智能回复
- The role of each layer in convolutional neural network
猜你喜欢

防范企业数据泄露,就用网络安全产品堡垒机!

Paging SQL (rownum, row_number, deny_rank, rank)

Pre war minesweeping: five measures for vulnerability management

PostgreSQL source code learning (24) -- transaction log ⑤ - log writing to wal buffer

13.TCP-bite

The way of enterprise transformation and upgrading: digital transformation, thinking first

C#学习二:堆和栈

Mingdeyang xilinx-k7-325t/410t core board data manual

About sql+nosql: newsql database

STM32与GD32笔记
随机推荐
Rust Basics
天谋科技 Timecho 完成近亿元人民币天使轮融资,围绕 Apache IoTDB 打造工业物联网原生时序数据库
postgresql源码学习(24)—— 事务日志⑤-日志写入WAL Buffer
MySQL development specification pdf
Symfony framework security component firewall configuration
radar transmitter
stlink故障修复
14.ip protocol -bite
面试官:说一下MySQL事务隔离级别?
Cmake learning-2
taro3.*中使用 dva 入门级别的哦
Kotlin annotation Statement and use
Applet judges that the data is not empty
swoole TCP 分布式实现
PostgreSQL source code learning (25) -- transaction log ⑥ - wait for log writing to complete
【力扣10天SQL入门】Day7+8 计算函数
关于 国产麒麟系统运行Qt,在命令行可以运行而双击无法运行(无反应) 的解决方法
CVPR 2022 | greatly reduce the manual annotation required for zero sample learning. Mapuosuo and Beiyou proposed category semantic embedding rich in visual information
The role of each layer in convolutional neural network
Taro2.* 小程序配置分享微信朋友圈