Because of Golang's origins, support for Linux is the best, and correspondingly, MAC support is good, and support for Windows is the worst.
Cross-compiling is best done from the source code, and in cross-compilation is not supported CGO, please note.
1. Access to Golang source code
This is, of course, downloaded from the official website, but Golang is the language that Google launched, so we need to turn over a wall. I downloaded 1.4.2 's Golang tar package and unzipped it. Although the configuration is correct, it can be placed in any location, but it is recommended to be placed in the/usr/local directory. Please note that this time there is no compile tool, not only cross-compiling not, local compilation is not.
Set the GOROOT environment variable to add paths inside path
2. Compiling the tool chain for this machine
CD $GOROOT \SRC
./make/bash
After compiling, you can see that the/usr/local/go directory has more than one bin directory, which contains the tools required for local compilation, this time the native compilation can be natively compiled.
In contrast, we can see the change very clearly.
3. Build cross-platform compilers and linker
The tool chain we generated earlier is for native, that is, for Mac, to support cross-compiling, obviously need to support the toolchain of other platforms, so this step is used to build such a tool chain.
This step is to run a Buildcmd script file with the contents of the file as shown below. Where 8x represents the 386 architecture, 6x represents the AMD64 architecture, and 5x represents the architecture of arm.
When this file is run, the compile and link tools under the specified schema are generated.
#!/bin/shset-efor Arch in 8 6 5; Do- cmd in a C-G l; Do go tool dist install-v cmd/$arch $cmd donedoneexit 0
4. Build standard command tools and libraries for the target version
As with this machine, we need to cross-compile which platform's content, we need to compile the target platform's tool chain. Let's take a look at some of the environment parameters shown in the Go Env run.
Here the Goarch and GOOS distribution represents the architecture and OS information for the target platform, Gohostarch and Gohostos represent the host's architecture and OS information.
Because we are in the MAC environment, now Gohostarch is Amd64,gohostos is Darwin, this is the same; the default Goarch and GOOS are native platforms, we want to cross-compile, to change these two parameters, Use the relevant toolchain to compile the link and generate the available image.
We are looking at the/usr/local/go/bin directory and we have just generated this directory and added it to the PATH environment variable. This directory contains a compilation tool, and obviously, you can only compile the machine at this time. We want to build a tool that compiles the target platform, so we run Make.bash again, but we need to specify Goarch and GOOS, assuming we need to compile the software under 32-bit Windows, the command is as follows:
Goarch=386 goos=windows./make.bash
If you have permission issues, please add sudo
This time we look at the/usr/local/go/bin directory, is not see the new add a windows_386 directory? What we have in store is the compilation tool we want.
5. Build a standard library of target versions
This step is to run a buildpkg script file with the following file contents:
#!/bin/shif [-Z "$"]; Then Echo ' GOOS was not specified ' 1>&2 exit 2else Export goos=$1 if ["$GOOS" = "windows"]; then
export cgo_enabled=0 Fifishiftif [-N "$"], then export GOARCH=$1FICD $GOROOTgo tool Dist Install-v. /pkggo Install-v-A STD
This script is used to pass the OS and arch parameters that need to be passed into the target environment using this script, to set the parameters of Goos and Goarch, and then to generate a standard library of links under the target platform. Or a 32 Windows example, the command is:
./buildpkg Windows 386
Similarly, if you have permission problems, use sudo
After the run, we look at the/usr/local/go/pkg directory, is not the addition of windows_386 directory? The corresponding library is stored inside.
6. Cross-compiling. Go source files
Once the preparation is complete, the source file can be formally compiled. Note that we still need to specify both GOOS and Goarch parameters, if not specified, will be considered to compile local files, continue to 32-bit Windows as an example, the command is as follows:
Cgo_enabled=0 goos=windows goarch=386 go build./hello.go
Where cgo_enabled=0 means that cgo,goos=windows is not used to indicate that the target platform is windows,goarch=386 represents a 32-bit system, and then the compiled result is the software running under the 32-bit Windows platform!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Cross-compiling for Golang under Mac