如何在Windows 10上交叉编译Go程序 [英] How to CrossCompile Go programs on Windows 10

查看:413
本文介绍了如何在Windows 10上交叉编译Go程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为Linux机器编译一个Go程序。我一直使用这里描述的方式:

I want to compile a Go programm for a linux machine. I always used that way which is described here:

如何从Windows交叉编译到Linux?

在Windows进行最后一次重大更新之前,效果一直很好10.现在,我无法设置



That worked pretty well until the last big update from Windows 10. Now I am not able to set the GOOS with

set GOOS=linux

我也尝试以管理员身份启动PowerShell,但即使这样也无法正常工作。
是否有任何工具可以做到这一点?还是有另一种方法可以在Windows 10上交叉编译Go程序?

I also tried to start the PowerShell as administrator, but even that is not working. Are there any tools which can do that? Or is there another way to crosscompile Go program on Windows 10?

推荐答案

set 是Windows命令行解释器的内部命令( cmd.exe )。

set is an internal command of the Windows command line interpreter (cmd.exe).

如果使用PowerShell,然后更改环境变量的值应该像这样:

If you're using PowerShell, then changing values of environment variables should be done like:

$Env:<variable-name> = "<new-value>"

有关更多详细信息,请参见 PowerShell文档:关于环境变量

For more details, see PowerShell documentation: About Environment Variables

因此要更改 GOOS ,请使用:

$Env:GOOS = "linux"

要进行交叉编译:


  1. 导航到 main 软件包所在的文件夹。

  1. Navigate to the folder where the main package is.

运行 $ Env:GOOS = linux

可选运行 $ Env:GOARCH = amd64

运行 go build

也可以在一行中完成:

$Env:GOOS = "linux"; $Env:GOARCH = "amd64"; go build

指定输出文件名:

$Env:GOOS = "linux"; $Env:GOARCH = "amd64"; go build -o hello

这篇关于如何在Windows 10上交叉编译Go程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆