How to cross compile CGO ARM architecture on Ubuntu

We all know that when it is not involved in the CGO, Go cross compiler is very simple, just set the corresponding GOOS and GOARCH can, but when it comes to CGO, the problem becomes a little more complicated, because you need to specify a particular GCC .

For example, to cross compile a dynamic library with CGO on Ubuntu, the target CPU architecture arm, how do we operate it?

Sample Code

# shared.go
package main

import "C"

//export Sum
func Sum(a, b int) int {
    return a + b
}

func main(){}

This code is used to CGO, and then exposed to a Sum method, implemented two integers.

Compiled 32 of the arm

Because by providing the open CGO compiler CGO_ENABLED = 1, run the following command:

CGO_ENABLED=1 GOOS=linux GOARCH=arm go build -buildmode=c-shared -o share.so 

Unfortunately, the command error: gcc: error: unrecognized command line option '-marm'.

As I mentioned in the beginning, cross compiler CGO choose the specific cross compiler tool arm and the upper arm Ubuntu compiler can use the 32-bit gcc-arm-linux-gnueabihf, install command as follows:

sudo apt-get update
sudo apt-get install gcc-arm-linux-gnueabihf

After installation is complete, specify CC recompile:

CGO_ENABLED=1 GOOS=linux GOARCH=arm CC=arm-linux-gnueabihf-gcc go build -buildmode=c-shared -o share.so 

Command to run successfully, then the directory has been generated file called share.so to view its properties through the file command, you can confirm that indeed arm 32 version.

share.so: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, BuildID[sha1]=7b23579ddcbebdfc8f4b68512859661a45d66996, not stripped

Compiled 64 of arm

Cross compile time, not only for the platform of choice for GCC, but also to choose the number of bits of the operating system, it is necessary to select 64 different GCC, recommended here gcc-linaro-5.3-2016.02-x86_64_aarch64-linux-gnu.tar .xz.

Installation command:

wget https://releases.linaro.org/components/toolchain/binaries/5.3-2016.02/aarch64-linux-gnu/gcc-linaro-5.3-2016.02-x86_64_aarch64-linux-gnu.tar.xz 

tar xvf gcc-linaro-5.3-2016.02-x86_64_aarch64-linux-gnu.tar.xz -C /usr/lib/

echo 'export PATH="$PATH:/usr/lib/gcc-linaro-5.3-2016.02-x86_64_aarch64-linux-gnu/bin"' >> ~/.bashrc

source ~/.bashrc

The installation is complete, re-compile command:

CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc-5.3.1 go build -buildmode=c-shared -o share.so 

Compile successful, and produces a share.so document, we use the same file share.so view the meta-information is:

share.so: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, BuildID[sha1]=5b0e7ff7c3af178039a7b934df8ca3e7572ab5b5, not stripped

So far, we have successfully cross-compiled an arm and arm64 two versions CGO programs on Ubuntu systems.

to sum up

Go when it comes to cross-compiling CGO, specify only GOOS and GOARCH is not enough, you also need to specify the appropriate version of GCC by CC parameters, GCC and the current system of selection and the target architecture related to:

  1. Cross compiler target CPU architecture (including 32-bit or 64-bit).
  2. Cross compiler where the operating system.

Guess you like

Origin blog.51cto.com/51reboot/2454221