Compile to Android

I wrote the below build.go to automate cross compiling process, but it fail Android, what could be the reason:

package main

import (
	"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",
		},
	}

	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 "android", "linux":
			output = "output"
		default:
			println("Undefined")
		}
		cmd := exec.Command("go", "build", "-o", output, "main.go")
		err := cmd.Run()
		if err != nil {
			println("cmd.Run() failed with %s\n", err)
		}
	}
}

I got the below output:

C:\Users\hasan\Documents\GoPlay\Env>go run build.go
darwin
android
cmd.Run() failed with %s
 (0xd9d6e0,0xc0000960e0)
linux

The builtin println function is deprecated and should not be used. Instead, use fmt.Println. In your case, because you’re using formatting directives, use fmt.Printf to change this:

println("cmd.Run() failed with %s\n", err)

to this:

fmt.Printf("cmd.Run() failed with %v\n", err)

On my computer, that gets me this message:

> go run .\main.go
darwin
android
cmd.Run() failed with exit status 2
linux

To get more information, check the stdout and stderr from the command:

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)
}

Which gives me the output:

> go run .\main.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: exec: "gcc": executable file not found in %PATH%


linux

Perhaps you’re getting a similar error.

1 Like

Thanks a lot for the details answer, in my case I got this error:

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'

I tried same at Mac OS, and got:

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)

And tried at Linux (Ubuntu WSL at Win10) and got:

# 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

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.