Go 1.7 从 Windows 到 Linux/Ubuntu 的交叉编译

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

Go 1.7 Cross Compilation from Windows to Linux/Ubuntu

问题

我在我的Windows 10上安装了GO 1.7。我创建了一个测试程序,在Windows上运行得很完美。下一步是尝试在我的Ubuntu虚拟机上运行它。

我在这里找到了一些关于如何做的信息:

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

我在cmd中运行了第1行和第2行,没有问题。在第3行,我遇到了一个错误:

go tool dist: open C:\Go\src\pkg\runtime: 系统找不到指定的路径。

我手动检查了这个文件夹,发现只有Windows的runtime文件。

问题是我在哪里以及如何下载适用于Linux的文件?或者也许我完全走错了路...

更新 09/02/2017

我按照建议运行了以下命令:

set GOARCH=amd64
set GOOS=linux
go build -o "myapp"

然后我将这个文件复制到共享文件夹,从那里复制到另一个非共享文件夹(为了避免在这里描述的问题),并执行了以下命令:

root@7dd1655ae5db:/__notshared# ./myapp
bash: ./myapp: 无法执行二进制文件: Exec format error

在下载文件包后,我检查了我的文件:

root@7dd1655ae5db:/__notshared# file myapp
myapp: PE32+ executable (console) x86-64 (stripped to external PDB), for MS Windows

似乎在构建过程中没有创建Linux可执行文件。

英文:

I have GO 1.7 installed on my Windows 10. I created test program and it works perfectly in Windows. Next step is to try to run it on my docker virtual machine with Ubuntu.

I found here some info about the way to do it

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

I run line 1 and 2 in cmd and there is no problem. At line 3 I have an error

go tool dist: open C:\Go\src\pkg\runtime: The system cannot find the path specified.

I check manually this folder and there is a runtime only for windows

Go 1.7 从 Windows 到 Linux/Ubuntu 的交叉编译

The question is where and how can I download it for linux? Or maybe thats I'm doing is completely wrong way...

UPDATE 09/02/2017

I ran like it was suggested

set GOARCH=amd64
set GOOS=linux
go build -o "myapp"

After I copied this file to shared folder, copied form there to another not shared folder (to avoid an issue described here) and executed

root@7dd1655ae5db:/__notshared# ./myapp
bash: ./myapp: cannot execute binary file: Exec format error

After I downloaded file package checked my file

root@7dd1655ae5db:/__notshared# file myapp
myapp: PE32+ executable (console) x86-64 (stripped to external PDB), for MS Windows

It seems that during build not linux executable was created.

答案1

得分: 9

那个问题有点旧了(来自2013年)。

跨平台支持已经有了很大的发展,你只需要更改环境变量(GOARCHGOOS)并运行go build命令。

首先进入main包所在的文件夹,然后执行以下命令:

set GOARCH=amd64
set GOOS=linux
go build

你也可以像这样更改结果二进制文件的名称:

go build -o "myapp"

请注意,在Linux上为Windows amd64编译应用程序,只需要一个命令就足够了(在main包所在的文件夹中):

GOOS=windows GOARCH=amd64 go build

这也在博客文章中得到了确认:Dave Cheney: Cross compilation with Go 1.5

> 使用Go 1.5进行交叉编译的过程如下:

> 1. 将GOOSGOARCH设置为目标操作系统和架构的值。

> 2. 运行go build -v YOURPACKAGE

注意事项

你不需要运行go install,也不应该运行它,因为它会编译并安装你的GOPATH中的包,这通常是不需要的。进行交叉编译不是为了开发/测试你的应用程序和包,而是为了生成一个在另一个系统/计算机上运行的单个二进制文件。

go build不会安装任何东西,它只会生成可执行的二进制文件。详情请参阅https://stackoverflow.com/questions/30612611/what-does-go-build-build

也在博客文章中得到了确认:Dave Cheney: Cross compilation with Go 1.5

> 在交叉编译时,应该使用go build,而不是go install。这是为数不多的几种情况之一,go buildgo install更可取。

> 原因是go install总是将编译的包(.a文件)缓存到与源代码根目录匹配的pkg/目录中。

英文:

That other question is a bit old (from 2013).

Cross-platform support evolved a lot, all you need to do is change the environment variables (GOARCH and GOOS) and run go build.

Navigate to the folder of the main package, then:

set GOARCH=amd64
set GOOS=linux
go build

You may change the name of the result binary like this:

go build -o "myapp"

Note that in Linux to compile the app for Windows amd64, a single command is enough (in the folder of the main package):

GOOS=windows GOARCH=amd64 go build

This is also confirmed in blog post: Dave Cheney: Cross compilation with Go 1.5:

> To cross compile a Go program using Go 1.5 the process is as follows:

> 1. set GOOS and GOARCH to be the values for the target operating system and architecture.

> 2. run go build -v YOURPACKAGE

Notes

You don't have to, and you shouldn't run go install, as that will compile and install the packages in your GOPATH, which is often not wanted. Doing cross compilation is not for developing / testing your app and packages. It is to produce a single binary which you will run on another system / computer.

go build will not install anything, it will just produce the executable binary. For details, see https://stackoverflow.com/questions/30612611/what-does-go-build-build

Also confirmed in blog post: Dave Cheney: Cross compilation with Go 1.5:

> When cross compiling, you should use go build, not go install. This is the one of the few cases where go build is preferable to go install.

> The reason for this is go install always caches compiled packages, .a files, into the pkg/ directory that matches the root of the source code.

答案2

得分: 7

我发现了一个问题以及解决方法。

在我的Windows 10中,无论是在命令提示符(cmd)还是在PowerShell控制台中,以下命令:

set GOARCH=amd64
set GOOS=linux

实际上没有起到任何作用!唯一有效的方法是需要打开控制面板 -> 系统 -> 高级系统设置 -> 环境变量,并在那里手动添加它们。

如果你在开发中使用Visual Studio Code,请不要忘记重新启动它。

更新 24/02/2017

除了上述方法,你可以在Windows PowerShell中设置变量,如下所示:

> $env:GOOS = "linux"

并将其输出到控制台:

> $env:GOOS

英文:

I found a problem and a solution of it.

In my Windows 10 these commands

set GOARCH=amd64
set GOOS=linux

in cmd and also in powershell console did really nothing! Only the way it works is that I need to open Control Panel -> System -> System Advanced Settings -> Environment Variables and add them there manually.

Go 1.7 从 Windows 到 Linux/Ubuntu 的交叉编译

If you use Visual Studio Code for development, do not forget to restart it.

UPDATE 24/02/2017

Instead all above you can set variable in windows powershell like this

> $env:GOOS = "linux"

and read it to console

> $env:GOOS

答案3

得分: 1

在Windows 10上,使用Go 1.14,以下方法适用:

go env -w GOARCH=amd64
go env -w GOOS=linux
go build -o test-linux test.go

这样就可以了。

我在Windows命令提示符和Windows PowerShell中都尝试过,结果没有区别。

如果你用文本编辑器打开二进制文件,你会看到它以ELF(Linux)开头,而不是MZ(Windows)。

我在一台具有给定架构的Linux机器上运行它(RHEL 7.3),它正常工作并给出了正确的输出。

将文件复制到Linux机器后,我需要将其设置为可执行文件:

$ chmod +x test-linux

然后我就可以运行它了:

$ ./test-linux

你也可以在Linux上运行以下命令,以获取有关该文件的更多详细信息:

$ file test-linux
test-linux: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

当然,如果你的架构已经是amd64,那么你就不需要再设置它了。你只需要设置GOOS即可。

在完成交叉编译后,你也可以将GOOS恢复为windows。当我关闭Windows命令提示符和PowerShell,并运行go env查看Go环境变量列表时,GOOS仍然保持为linux的值。我没有尝试重新启动Windows。所以,如果你现在想再次编译为Windows:

go env -w GOOS=windows

请注意使用正确的大小写,如上所示。例如,WindowsLinux是不起作用的。

英文:

What worked for me, on Windows 10 with Go 1.14, is the following:

go env -w GOARCH=amd64
go env -w GOOS=linux
go build -o test-linux test.go

That's it.

I tried this in both Windows command prompt and Windows PowerShell. There's no difference in results.

If you open the binary file in a text editor, you should see that it begins with ELF (Linux), and not MZ (Windows).

I ran it on a Linux machine (RHEL 7.3) with the given architecture, and it worked properly. It gave correct output.

After copying the file to the Linux machine, I had to make it executable:

$ chmod +x test-linux

Then I was able to run it:

$ ./test-linux

You can also run the following command on Linux, to get more detail about the file:

$ file test-linux
test-linux: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

Of course, if your architecture is amd64 already, then you don't have to set it again. You can just set the GOOS.

You can also revert GOOS to windows after you're done with cross-compiling. When I closed the Windows command prompt and PowerShell, and ran go env to see the list of Go environment variables, and GOOS kept the linux value. I didn't try with restarting Windows. So, if you now want to compile for Windows again:

go env -w GOOS=windows

You need to use the correct casing, as shown here. For example, Windows or Linux won't work.

huangapple
  • 本文由 发表于 2017年2月9日 00:02:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/42117934.html
匿名

发表评论

匿名网友

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

确定