在Windows 7(32位)上编译适用于所有平台的Golang代码。

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

Golang compile for all platforms in Windows 7 (32 bit)

问题

我正在使用Windows 7 [32位]操作系统。

我正在构建一个示例Go程序。

我想要从我的Windows 7 [32位]操作系统编译这个程序,使其适用于所有平台。

我想要为所有的Linux [32/64] / Mac OSX [32/64] / Windows[32/64]编译我的程序。

从我的单一操作系统中是否可能实现这一点?

英文:

I'm using windows 7 [32 bit] operating system.

I'm build example go program.

I want to compile this program for all platforms from my windows 7 [32 bit] OS.

I want to compile my program for all Linux [32/64] / Mac OSX [32/64] / Windows[32/64].

Is it possible are not from my single OS ?

答案1

得分: 12

更新Go 1.5:请参阅“Go 1.5中的交叉编译变得更好了

> 要成功进行交叉编译,您需要:

> - 目标平台的编译器,如果它们与您的主机平台不同,例如您在darwin/amd64(6g)上,想要为linux/arm(5g)进行编译。

  • 目标平台的标准库,其中包括在构建Go发行版时生成的一些文件。

> 随着将Go编译器转换为Go的计划在1.5版本中实现,第一个问题现在已得到解决。

package main

import "fmt"
import "runtime"

func main() {
        fmt.Printf("Hello %s/%s\n", runtime.GOOS, runtime.GOARCH)
}

> 为darwin/386构建

% env GOOS=darwin GOARCH=386 go build hello.go
# scp到darwin主机
$ ./hello
Hello darwin/386

> 或者为linux/arm构建

% env GOOS=linux GOARCH=arm GOARM=7 go build hello.go
# scp到linux主机
$ ./hello
Hello linux/arm

原始答案(适用于Go 1.4及之前版本)

您可以尝试使用gox等工具

> Gox是一个简单的、无花俏的Go交叉编译工具,行为类似于标准的go build。
Gox将为多个平台并行构建。
Gox还将为您构建交叉编译工具链。

> 在使用Gox之前,您必须构建交叉编译工具链。Gox可以为您完成这个过程。这只需要执行一次(或者在更新Go时执行):

$ gox -build-toolchain

您还可以在“使用Go进行多平台开发”中找到许多跨平台开发的技巧。

Passionate Developer在下面指出了问题19,由OP Nakka Chandra留下,尽管问题10报告了在Windows上成功运行gox的情况。

英文:

Update Go 1.5: see "Cross compilation just got a whole lot better in Go 1.5"

> For successful cross compilation you would need

> - compilers for the target platform, if they differed from your host platform, ie you’re on darwin/amd64 (6g) and you want to compile for linux/arm (5g).

  • a standard library for the target platform, which included some files generated at the point your Go distribution was built.

> With the plan to translate the Go compiler into Go coming to fruition in the 1.5 release the first issue is now resolved.

package main

import "fmt"
import "runtime"

func main() {
        fmt.Printf("Hello %s/%s\n", runtime.GOOS, runtime.GOARCH)
}

> build for darwin/386

% env GOOS=darwin GOARCH=386 go build hello.go
# scp to darwin host
$ ./hello
Hello darwin/386

> Or build for linux/arm

% env GOOS=linux GOARCH=arm GOARM=7 go build hello.go
# scp to linux host
$ ./hello
Hello linux/arm

Original answer (Go 1.4 and before)

You can try a tool like gox

> Gox is a simple, no-frills tool for Go cross compilation that behaves a lot like standard go build.
Gox will parallelize builds for multiple platforms.
Gox will also build the cross-compilation toolchain for you.

> Before you use Gox, you must build the cross-compilation toolchain. Gox can do this for you. This only has to be done once (or whenever you update Go):

$ gox -build-toolchain

You will also find many cross-platform development tips at "Developing for Multiple Platforms With Go".

Passionate Developer points out below to issue 19, left by the OP Nakka Chandra, even though issue 10 reported making gox run successfully on Windows.

答案2

得分: 3

在Windows上运行以下命令:

C:\Users\user\go\src\myapp> set GOOS=linux  
C:\Users\user\go\src\myapp> set GOARCH=amd64  
C:\Users\user\go\src\myapp> go build  

这对我有效。

注意,如果你遇到以下错误:

> cmd/go: unsupported GOOS/GOARCH pair linux/amd64

这是因为变量末尾有一个空格。
例如,错误的使用方式是:set GOOS=linux<space>),正确的方式是:set GOOS=linux

这是所有其他系统的完整表格列表(取自这里):

    GOOS - 目标操作系统|	GOARCH - 目标平台
   -------------------------------|--------------------------
   |           android	          |           arm           |  
   |           darwin	          |           386           |  
   |           darwin	          |           amd64         |    
   |           darwin	          |           arm           |  
   |           darwin	          |           arm64         |    
   |           dragonfly	      |           amd64         |    
   |           freebsd	          |           386           |  
   |           freebsd	          |           amd64         |    
   |           freebsd	          |           arm           |  
   |           linux	          |           386           |  
   |           linux	          |           amd64         |    
   |           linux	          |           arm           |  
   |           linux	          |           arm64         |    
   |           linux	          |           ppc64         |    
   |           linux	          |           ppc64le       |      
   |           linux	          |           mips          |   
   |           linux	          |           mipsle        |     
   |           linux	          |           mips64        |     
   |           linux	          |           mips64le      |       
   |           netbsd	          |           386           |  
   |           netbsd	          |           amd64         |    
   |           netbsd	          |           arm           |  
   |           openbsd	          |           386           |  
   |           openbsd	          |           amd64         |    
   |           openbsd	          |           arm           |  
   |           plan9	          |           386           |  
   |           plan9	          |           amd64         |    
   |           solaris	          |           amd64         |    
   |           windows	          |           386           |   
   |           windows	          |           amd64         |
   ----------------------------------------------------------
英文:

On Windows run the following commands:

C:\Users\user\go\src\myapp> set GOOS=linux  
C:\Users\user\go\src\myapp> set GOARCH=amd64  
C:\Users\user\go\src\myapp> go build  

It worked for me.

Notice, if you get the error:

> cmd/go: unsupported GOOS/GOARCH pair linux/amd64

This is because you have a space at the end of the variable.
Example, wrong use is: set GOOS=linux<space>), instead it should be: set GOOS=linux.

This is the full table list (taken from here) for all the other systems:

    GOOS - Target Operating System|	GOARCH - Target Platform
   -------------------------------|--------------------------
   |           android	          |           arm           |  
   |           darwin	          |           386           |  
   |           darwin	          |           amd64         |    
   |           darwin	          |           arm           |  
   |           darwin	          |           arm64         |    
   |           dragonfly	      |           amd64         |    
   |           freebsd	          |           386           |  
   |           freebsd	          |           amd64         |    
   |           freebsd	          |           arm           |  
   |           linux	          |           386           |  
   |           linux	          |           amd64         |    
   |           linux	          |           arm           |  
   |           linux	          |           arm64         |    
   |           linux	          |           ppc64         |    
   |           linux	          |           ppc64le       |      
   |           linux	          |           mips          |   
   |           linux	          |           mipsle        |     
   |           linux	          |           mips64        |     
   |           linux	          |           mips64le      |       
   |           netbsd	          |           386           |  
   |           netbsd	          |           amd64         |    
   |           netbsd	          |           arm           |  
   |           openbsd	          |           386           |  
   |           openbsd	          |           amd64         |    
   |           openbsd	          |           arm           |  
   |           plan9	          |           386           |  
   |           plan9	          |           amd64         |    
   |           solaris	          |           amd64         |    
   |           windows	          |           386           |   
   |           windows	          |           amd64         |
   ----------------------------------------------------------

答案3

得分: 1

您可以通过运行go tool dist list命令找到您的go所支持的完整的GOOS/GOARCH平台列表。

对于Go 1.18.1版本,根据您所感兴趣的目标操作系统进行过滤后,以下是显示的配对列表:

$ go tool dist list | grep -e linux -e darwin -e windows
darwin/amd64
darwin/arm64
linux/386
linux/amd64
linux/arm
linux/arm64
linux/mips
linux/mips64
linux/mips64le
linux/mipsle
linux/ppc64
linux/ppc64le
linux/riscv64
linux/s390x
windows/386
windows/amd64
windows/arm
windows/arm64

如果您想为所有这些平台编译您的代码,您可以使用下面的bash/sh脚本来实现:

bin_name='example'  # 根据需要更改二进制文件名

go tool dist list | grep -e linux -e darwin -e windows | while read -r pair; do
    # 从配对中提取GOOS和GOARCH
    GOOS="${pair%/*}"
    GOARCH="${pair#*/}"

    suffix=''
    # 如果GOOS是windows,则设置后缀为.exe
    [ "$GOOS" = 'windows' ] && suffix='.exe'

    # 将<args>替换为需要的适当参数
    GOOS="$GOOS" GOARCH="$GOARCH" \
        go build -o "${bin_name}-${GOOS}-${GOARCH}${suffix}" <args>
done

该脚本将为每个操作系统/架构配对生成一个可执行文件,并在名称末尾附加操作系统和架构。如果操作系统是Windows,则还会附加.exe后缀。

英文:

You can find the full list of supported GOOS/GOARCH platforms supported by your go by running go tool dist list.

For Go 1.18.1, this shows the following pairs, after filtering based on the target OSes you said you are interested in:

<pre>
$ go tool dist list | grep -e linux -e darwin -e windows
darwin/amd64
darwin/arm64
linux/386
linux/amd64
linux/arm
linux/arm64
linux/mips
linux/mips64
linux/mips64le
linux/mipsle
linux/ppc64
linux/ppc64le
linux/riscv64
linux/s390x
windows/386
windows/amd64
windows/arm
windows/arm64
</pre>

If you want to compile your code for all of these, you can do this with the bash/sh script below:

bin_name=&#39;example&#39;  # Change the binary name as desired

go tool dist list | grep -e linux -e darwin -e windows | while read -r pair; do
    # Extract the GOOS and GOARCH from the pair
    GOOS=&quot;${pair%/*}&quot;
    GOARCH=&quot;${pair#*/}&quot;

    suffix=&#39;&#39;
    # If GOOS is windows, set suffix to .exe
    [ &quot;$GOOS&quot; = &#39;windows&#39; ] &amp;&amp; suffix=&#39;.exe&#39;

    # Replace &lt;args&gt; with appropriate arguments as needed
    GOOS=&quot;$GOOS&quot; GOARCH=&quot;$GOARCH&quot; \
        go build -o &quot;${bin_name}-${GOOS}-${GOARCH}${suffix}&quot; &lt;args&gt;
done

This script will produce an executable for each OS/architecture pair, and append the OS and architecture at the end of the name. It will also append .exe if the OS is Windows.

huangapple
  • 本文由 发表于 2014年7月31日 13:47:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/25051623.html
匿名

发表评论

匿名网友

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

确定