windows环境下安装make,并且能在goland中顺利执行makefile
1.管理员运行powershell,执行:
Set-ExecutionPolicy Bypass -Scope Process -Force; `
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; `
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
2.安装make命令:
choco install make
3.安装完成后重启终端,在 cmd 或 PowerShell 中运行:
make --version
4.确认 make.exe 安装位置,一般位置是:
C:\ProgramData\chocolatey\bin\make.exe
5.把 make.exe 路径加入系统 PATH 环境变量(Git Bash 也能识别)
6.如果goland还无法执行makefile则在File > Settings > Build> Build Tools>Make中设置make的安装即可
makefile示例:
# 声明伪目标,避免与同名文件冲突
.PHONY: all proto build run clean
# 应用名称(最终编译出来的二进制文件名)
APP_NAME = my-app
# Go 源码文件(支持子目录)
SRC = main.go internal/*.go
# .proto 文件所在目录
PROTO_DIR = ./pb
# 生成的 Go 文件输出目录
GEN_DIR = gen
# 构建时间(用 shell 获取当前时间)
TIME = $(shell date "+%Y-%m-%d %H:%M")
# Git 提交哈希(获取当前 commit)
GIT_COMMIT = $(shell git rev-parse --short HEAD)
# Go 版本(只取第三个单词,比如 "go1.21.0")
GO_VER = $(word 3, $(shell go version))
# 默认执行目标:等价于 make all -> 执行 proto + build
all: proto build
# ====== 目标:编译 .proto 文件生成 go 源码 ======
proto:
@echo "Compiling .proto files..."
-@mkdir -p $(GEN_DIR)
@protoc --proto_path=$(PROTO_DIR) --go_out=$(GEN_DIR) --go-grpc_out=$(GEN_DIR) $(PROTO_DIR)/user.proto
# ====== 目标:构建 Go 可执行文件 ======
build:
@echo "Building $(APP_NAME)..."
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build \
-ldflags "-X 'main.BuildTime=$(TIME)' -X 'main.GitCommit=$(GIT_COMMIT)' -X 'main.GoVersion=$(GO_VER)'" \
-o $(APP_NAME) \
$(SRC)
# ====== 目标:本地运行项目 ======
run:
@echo "Running app..."
@go run $(SRC)
# ====== 目标:清理构建产物 ======
clean:
@echo "Cleaning..."
@go clean # 清理 go 缓存、对象文件等
@rm -f $(APP_NAME) # 删除可执行文件
.PHONY: all proto build run clean
# 应用名称(最终编译出来的二进制文件名)
APP_NAME = my-app
# Go 源码文件(支持子目录)
SRC = main.go internal/*.go
# .proto 文件所在目录
PROTO_DIR = ./pb
# 生成的 Go 文件输出目录
GEN_DIR = gen
# 构建时间(用 shell 获取当前时间)
TIME = $(shell date "+%Y-%m-%d %H:%M")
# Git 提交哈希(获取当前 commit)
GIT_COMMIT = $(shell git rev-parse --short HEAD)
# Go 版本(只取第三个单词,比如 "go1.21.0")
GO_VER = $(word 3, $(shell go version))
# 默认执行目标:等价于 make all -> 执行 proto + build
all: proto build
# ====== 目标:编译 .proto 文件生成 go 源码 ======
proto:
@echo "Compiling .proto files..."
-@mkdir -p $(GEN_DIR)
@protoc --proto_path=$(PROTO_DIR) --go_out=$(GEN_DIR) --go-grpc_out=$(GEN_DIR) $(PROTO_DIR)/user.proto
# ====== 目标:构建 Go 可执行文件 ======
build:
@echo "Building $(APP_NAME)..."
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build \
-ldflags "-X 'main.BuildTime=$(TIME)' -X 'main.GitCommit=$(GIT_COMMIT)' -X 'main.GoVersion=$(GO_VER)'" \
-o $(APP_NAME) \
$(SRC)
# ====== 目标:本地运行项目 ======
run:
@echo "Running app..."
@go run $(SRC)
# ====== 目标:清理构建产物 ======
clean:
@echo "Cleaning..."
@go clean # 清理 go 缓存、对象文件等
@rm -f $(APP_NAME) # 删除可执行文件

浙公网安备 33010602011771号