2 回答

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 人,我很樂意學習和改進它。
- 2 回答
- 0 關注
- 218 瀏覽
添加回答
舉報