首页 > 解决方案 > 编译到安卓

问题描述

我写了下面的代码build.go来自动化交叉编译过程,但它失败Android了,可能是什么原因:

package main

import (
    "fmt"
    "os"
    "os/exec"
)

type compiles struct {
    sys  string
    arch string
}

func main() {
    var output string
    // go tool dist list
    systems := []compiles{
        compiles{
            sys:  "darwin",
            arch: "amd64",
        },
        compiles{
            sys:  "android",
            arch: "amd64",
        },
        compiles{
            sys:  "linux",
            arch: "amd64",
        },
        compiles{
            sys:  "windows",
            arch: "amd64",
        },
    }

    for _, s := range systems {
        os.Setenv("GOOS", s.sys)
        os.Setenv("GOARCH", s.arch)
        switch println(s.sys); s.sys {
        case "darwin":
            output = "output.dmg"
        case "windows":
            output = "output.exe"
        case "android", "linux":
            output = "output"
        default:
            println("Undefined")
        }
        cmd := exec.Command("go", "build", "-o", output, "main.go")
        out, err := cmd.CombinedOutput()
        if err != nil {
            fmt.Printf("cmd.Run() failed with %v:\n\noutput:\n\n%s\n", err, out)
        }
    }
}

我得到以下输出:

C:\Users\hasan\Documents\GoPlay\Env>go run build.go
darwin
android
cmd.Run() failed with exit status 2:

output:

# command-line-arguments
c:\go\pkg\tool\windows_amd64\link.exe: running gcc failed: exit status 1
gcc: error: unrecognized command line option '-rdynamic'


linux
windows

我在 Mac OS 上也试过,得到:

cmd.Run() failed with exit status 2:

output:

# command-line-arguments
/usr/local/go/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: unknown option: -z
clang: error: linker command failed with exit code 1 (use -v to see invocation)

并在 Linux(Win10 上的 Ubuntu WSL)上尝试并得到:

# command-line-arguments
/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/tmp/go-link-178415548/go.o:(.data+0x0): undefined reference to `x_cgo_callers'
/tmp/go-link-178415548/go.o:(.data+0x8): undefined reference to `x_cgo_init'
/tmp/go-link-178415548/go.o:(.data+0x10): undefined reference to `x_cgo_mmap'
/tmp/go-link-178415548/go.o:(.data+0x18): undefined reference to `x_cgo_munmap'
/tmp/go-link-178415548/go.o:(.data+0x20): undefined reference to `x_cgo_notify_runtime_init_done'
/tmp/go-link-178415548/go.o:(.data+0x28): undefined reference to `x_cgo_sigaction'
/tmp/go-link-178415548/go.o:(.data+0x30): undefined reference to `x_cgo_thread_start'
/tmp/go-link-178415548/go.o:(.data+0x38): undefined reference to `x_cgo_setenv'
/tmp/go-link-178415548/go.o:(.data+0x40): undefined reference to `x_cgo_unsetenv'
/tmp/go-link-178415548/go.o:(.data+0x48): undefined reference to `_cgo_yield'
collect2: error: ld returned 1 exit status

标签: go

解决方案


推荐阅读