文章目录
前言
GO语言的go命令自带源码管理功能,但是实际项目开发中满足不了需求。Makefile功能更强大,可以实现编译,安装,清理等功能。
提示:以下是本篇文章正文内容,下面案例可供参考
一、核心内容
介绍 Makefile
介绍如何使用 Makefile
二、Makefile简介
一个工程中的源文件不计其数,其按类型、功能、模块分别放在若干个目录中,Makefile 定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能操作,因为 Makefile 就像一个 Shell 脚本一样,其中也可以执行操作系统的命令(摘自百度百科)。
Makefile基本格式如下:
target ... : prerequisites ...
command
...
其中:
target: -编译生成的目标
prerequisites:-编译文件需要的依赖
command:-依赖生成目标所需要执行的命令(任意的shell命令),Makefile
中的命令必须以[tab]开头
比如我们平时使用的 gcc a.c b.c -o test 这里的 test 就是我们要生成的目标, a.c、b.c就是我们生成目标需要的依赖,而 gcc a.c b.c -o test 则是命令。将这行命令用 Makefile 的方式来写就是:
test: a.c b.c
gcc a.c b.c -o test
三、API Server 添加Makefile
在 apiserver 根目录下新建文件 Makefile,内容为:
all: gotool
@go build -v .
clean:
rm -f apiserver
find . -name "[._]*.s[a-w][a-z]" | xargs -i rm -f {}
gotool:
gofmt -w .
go tool vet . |& grep -v vendor;true
ca:
openssl req -new -nodes -x509 -out conf/server.crt -keyout conf/server.key -days 3650 -subj "/C=DE/ST=NRW/L=Earth/O=Random Company/OU=IT/CN=127.0.0.1/emailAddress=xxxxx@qq.com"
help:
@echo "make - compile the source code"
@echo "make clean - remove binary file and vim swp files"
@echo "make gotool - run go tool 'fmt' and 'vet'"
@echo "make ca - generate ca files"
.PHONY: clean gotool ca help
make:执行 go build -v . 生成 Go 二进制文件
make gotool:执行 gofmt -w . 和 go tool vet .(格式化代码和源码静态检查)
make clean:做一些清理工作:删除二进制文件、删除 vim swp 文件
make ca:生成证书
make help:打印 help 信息