交叉编译:“GOOS=linux”一词未被识别为名称 cmdlet。

huangapple go评论82阅读模式
英文:

Cross compilation "The term 'GOOS=linux' is not recognized as a name cmdlet"

问题

我正在尝试交叉编译我开发的应用程序,但是我遇到了以下错误:

错误:

GOOS=linux : 术语“GOOS=linux”无法识别为名称 cmdlet、函数、脚本文件或可操作程序。请检查名称的拼写,或者如果包含路径,请检查路径是否正确,然后重试。

我尝试使用以下代码进行编译:

GOOS=linux GOARCH=amd64 go build main.go

当我运行go env命令时,我的GOOS变量似乎已设置,显示如下:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\apoi\AppData\Local\go-build
set GOENV=C:\Users\apoi\AppData\Roaming\go\env
set GOEXE=
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\apoi\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=linux
set GOPATH=C:\Users\apoi\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.19.4
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=C:\Users\apoi\Desktop\azure-function-curso\go.mod
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-fPIC -m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\apoi\AppData\Local\Temp\go-build584110605=/tmp/go-build -gno-record-gcc-switches

我还尝试通过 Visual Studio Code 的 PowerShell 分别运行我的变量。我尝试的命令如下:

set GOARCH=amd64
set GOOS=linux
go tool dist install -v pkg/runtime
go install -v -a std
英文:

I'm trying to cross-compile an application I've developed.
but i am getting this error:

error:

GOOS=linux : The term 'GOOS=linux' is not recognized as a name
cmdlet, function, script file, or operable program. check the
spelling of the name or, if a path was included, see if the
path is correct and try again.

I'm trying to compile with the following code:

 GOOS=linux GOARCH=amd64 go build main.go

my GOOS variable seems to be set
when I run the go env command, this appears:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\apoi\AppData\Local\go-build
set GOENV=C:\Users\apoi\AppData\Roaming\go\env
set GOEXE=
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\apoi\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=linux
set GOPATH=C:\Users\apoi\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.19.4
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=C:\Users\apoi\Desktop\azure-function-curso\go.mod
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-fPIC -m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\apoi\AppData\Local\Temp\go-build584110605=/tmp/go-build -gno-record-gcc-switches

I've also tried running my variables separately, all through the powershell of visual studio code.
The attempt I made was this:

set GOARCH=amd64
set GOOS=linux
go tool dist install -v pkg/runtime
go install -v -a std

答案1

得分: 1

在PowerShell中,环境变量存储在$Env命名空间中,并且可以通过以下方式设置:

$Env:GOOS = 'linux'
$Env:GOARCH = 'amd64'

此外,我不知道是否有一种简单的方法可以像POSIX的sh那样仅在单个命令运行时更改环境,例如:

VAR=value command

你可能需要启动一个单独的子shell来实现这一点,类似于下面这段(未经测试)代码:

pwsh -Command { $Env:GOOS = 'linux' ; $Env:GOARCH = 'amd64' ; go build main.go }
英文:

In PowerShell, environment variables live in the $Env namespace, and are set like this:

$Env:GOOS = 'linux'
$Env:GOARCH = 'amd64'

Also, I am not aware of a simple way to change the environment for only a single run of a command the same way the POSIX sh allows with the

VAR=value command

syntax. You'd probably have to start a separate sub-shell for this, something like this (totally untested) code:

pwsh -Command { $Env:GOOS = 'linux' ; $Env:GOARCH = 'amd64' ; go build main.go }

答案2

得分: 0

如果在使用VS Code或PowerShell终端执行Go代码时遇到错误,请尝试使用Git Bash。Git Bash可能能够提供您所需的结果。

英文:

If you're encountering errors when using the terminal in VS Code or PowerShell to execute Go code, try using Git Bash instead. Git Bash may be able to provide you with the result you need.

huangapple
  • 本文由 发表于 2023年1月2日 21:24:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/74982973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定