Makefile 未设置 GOOS 环境变量

Makefile not set GOOS environment variable

我在 Makefile 下面写了为不同平台构建 Golang 代码。 (我的 OS 是 Windows 10 和 运行 通过命令提示符生成的 Makefile)

GOCMD   =  go
GOBUILD =  $(GOCMD)  build
GOFILES =   $(wildcard *.go)
SONG_PATH =  ./song-service
SONG_PATH_OUTPOUT =  ./song-service/cmd
SONG_BINARY_NAME_LIN =  songservice_lin

song-build-lin:
    set GOOS=linux
    set GOARCH=amd64
    $(GOBUILD)  -o "$(SONG_PATH_OUTPOUT)/$(SONG_BINARY_NAME_LIN)" -v "$(SONG_PATH)/$(GOFILES)"

当我 运行 make song-build-lin 它 运行 没有错误但是 GOOS 变量没有设置。 但是当我 运行 set GOOS=linux 在命令提示符下直接工作时!

终于找到答案了,

我运行使用VSCode终端发出命令,我使用的终端类型是命令提示符。如果将终端类型更改为 Git bash,命令将正常工作。

这是一个很好的问题,也是一个很棒的话题。
我被迫使用 Windows 用于开发的工作目的。我有一些队友使用 Windows,还有一些使用 Mac,还有一些人更愿意在 Linux 上工作但无法做到,我们刚得到一些 Linux 我们需要将可执行文件编译到的服务器。我最近浏览了 Makefile 教程网站 https://makefiletutorial.com/,他们有一个关于导出变量的部分。
在我的 windows 机器上,我能够使用 shell、cygwin。 下面是我能够使用的 make 文件中的编译...


# compile - cross compile all platforms
compile:
    @echo " === Compiling for every OS and Platform === "
    make compile-linux
    make compile-win 
    make compile-mac
    @echo " === COMPLETED! === "

# compile-linux - compile and create a linux executable for 64bit architecture
# NOTE: this sets the GOOS and GOARCH just for this makefile rule
compile-linux: export GOOS=linux
compile-linux: export GOARCH=amd64
compile-linux:
    go build -o bin/executable-linux.exe

# compile-win - compile and create a windows executable for 64bit architecture 
compile-win: export GOOS=windows
compile-win: export GOARCH=amd64
compile-win:
    go build -o bin/executable-win.exe

# compile-mac - compile and create a mac os executable for 64bit architecture 
compile-mac: export GOOS=darwin
compile-mac: export GOARCH=amd64
compile-mac:
    go build -o bin/executable-mac

这似乎对我有用,使用 Powershell 7 and/or Windows 命令行,以及我使用 Mac[=32= 的队友].
这是通用的,可以添加到任何地方使用。

对于这个解决方案(虽然它已经回答了 2 年多)可能是这样的...

GOCMD   =  go
GOBUILD =  $(GOCMD)  build
GOFILES =   $(wildcard *.go)
SONG_PATH =  ./song-service
SONG_PATH_OUTPOUT =  ./song-service/cmd
SONG_BINARY_NAME_LIN =  songservice_lin

song-build-lin: export GOOS=linux
song-build-lin: export GOARCH=amd64
song-build-lin:
    $(GOBUILD)  -o "$(SONG_PATH_OUTPOUT)/$(SONG_BINARY_NAME_LIN)" -v "$(SONG_PATH)/$(GOFILES)"

我很想知道这对您和其他人是否有效,以及是否还有其他改进。我不是一个大的 Makefile 人,我很乐意学习和改进它。