为了账号安全,请及时绑定邮箱和手机立即绑定

Makefile 未设置 GOOS 环境变量

Makefile 未设置 GOOS 环境变量

Go
四季花海 2023-06-19 15:50:23
我在 Makefile 下面写了为不同平台构建 Golang 代码。(我的操作系统是 Windows 10 并通过命令提示符运行 Makefile)GOCMD   =  goGOBUILD =  $(GOCMD)  buildGOFILES =   $(wildcard *.go)SONG_PATH =  ./song-serviceSONG_PATH_OUTPOUT =  ./song-service/cmdSONG_BINARY_NAME_LIN =  songservice_linsong-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直接在命令提示符下运行时它起作用了!
查看完整描述

2 回答

?
蝴蝶刀刀

TA贡献1801条经验 获得超8个赞

终于找到答案了

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


查看完整回答
反对 回复 2023-06-19
?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

这是一个很好的问题,也是一个很好的话题。

我被迫将 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 和/或 Windows 命令行,以及我使用 MacOS 的队友。

这是通用的,可以添加到任何地方使用。


对于这个解决方案(虽然它已经回答了 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 人,我很乐意学习和改进它。


查看完整回答
反对 回复 2023-06-19
  • 2 回答
  • 0 关注
  • 124 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信