Go language involves CGO cross compilation (cross platform compilation) solution

Posted by xwin on Fri, 04 Mar 2022 21:51:47 +0100

In actual development, we often use cross compilation [1] to realize cross platform deployment. After consulting the materials these two days, we found that TensorFlow cross platform compilation failed because CGO was used to call TensorFlow C. Today, let's have an in-depth understanding of how to deal with cross compilation with CGO.

Without CGO call, cross compilation can be realized with only three parameters

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
Or add optional parameters

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags '-s -w --extldflags "-static -fpic"' main.go
CGO_ The parameter enabled is 1 by default, enabling CGO. You need to specify 0 to close because CGO does not support cross compilation.

GOOS and GOARCH are used to specify that the platform to be built is Linux

Optional parameter - ldflags is the compilation option:

-s -w removing debugging information can reduce the file volume after construction,
– extldflags "- static -fpic" complete static compilation [2], so that the compiled files can be run on the specified platform without running environment configuration.
Obviously, for cross compilation with CGO, CGO_ENABLED must be on. This requires an auxiliary compiler to help us realize cross compilation.

If you are a mac platform, you can use this tool filosottile / musl Cross / musl cross to install directly through brew

brew install FiloSottile/musl-cross/musl-cross
After successful installation, there are many compilers that can help us realize cross compilation. When using, you only need to specify under the corresponding parameters of compilation.

FiloSottile/musl-cross/musl-cross
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC=x86_64-linux-musl-gcc CGO_LDFLAGS="-static" go build -a -v
Pass CC = x86_ 64 Linux musl GCC to specify gcc compiler. And CGO_LDFLAGS="-static" to specify the compilation of CGO part as static compilation.

In this way, cross compilation with CGO can be realized. If you are on other platforms, you can also download the corresponding platform tools through the cross platform compilation tool musl. Here are tools that support multi platform implementation. Download and unzip the bin file PATH in the unzipped directory and put it into the PATH environment variable.

brew install FiloSottile/musl-cross/musl-cross
The following problems occurred during installation:
Error: An exception occurred within a child process: NoMethodError: undefined method `path' for nil:NilClass Did you mean? paths
Solution
Use the command brew update reset to upgrade Homebrew [1]

References
[1] https://discourse.brew.sh/t/homebrew-installation-fail/7439

Install brew install filosottile / musl Cross / musl cross again
Tips are as follows:

lizhongsu@lizhongdeMBP ~ % brew install FiloSottile/musl-cross/musl-cross
==> Downloading https://ghcr.io/v2/homebrew/portable-ruby/portable-ruby/blobs/sha256:b065e5e3783954f3e65d8d3a6377ca51649bfcfa21b356b0dd70490f74c6bd86
##################################################################################################################################################################################################### 100.0%
==> Pouring portable-ruby-2.6.3_2.yosemite.bottle.tar.gz
Error:
  homebrew-core is a shallow clone.
To `brew update`, first run:
  git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
This command may take a few minutes to run due to the large size of the repository.
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for
the inconvenience!
==> Installing musl-cross from filosottile/musl-cross
==> Downloading https://f001.backblazeb2.com/file/filippo-public/musl-cross-0.9.9_1.catalina.bottle.tar.gz
######################################################################## 100.0%
==> Pouring musl-cross-0.9.9_1.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/musl-cross/0.9.9_1: 1,851 files, 246.3MB
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /Users/lizhongsu/Library/Caches/Homebrew/autoconf--2.69.catalina.bottle.4.tar.gz... (871.6KB)
Removing: /Users/lizhongsu/Library/Caches/Homebrew/automake--1.16.2.catalina.bottle.tar.gz... (948.7KB)
Removing: /Users/lizhongsu/Library/Logs/Homebrew/autoconf... (64B)
Removing: /Users/lizhongsu/Library/Logs/Homebrew/automake... (64B)
Removing: /Users/lizhongsu/Library/Logs/Homebrew/telnet... (64B)
Pruned 2 symbolic links from /usr/local

There is an error prompt:
Error:
homebrew-core is a shallow clone.
To brew update, first run:
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
This command may take a few minutes to run due to the large size of the repository.
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for
the inconvenience!

It has not been processed according to the prompt for the time being. Try to compile and succeed!
Execute command
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC=x86_64-linux-musl-gcc CGO_LDFLAGS="-static" go build -a -v

Successfully cross compile the executable files of Go under linux.

total 149968
    8 -rw-rw-r--@  1 lizhongsu  staff       697 Oct 10  2020 Dockerfile
98320 -rw-------@  1 lizhongsu  staff  50152502 Mar  1 16:53 EasyRTC.zip
    8 -rw-rw-r--@  1 lizhongsu  staff      2244 Oct 10  2020 README.md
51416 -rwxr-xr-x   1 lizhongsu  staff  25711520 Apr 17 03:53 adminserver
    0 drwxrwxr-x@  5 lizhongsu  staff       160 Oct 10  2020 app
    8 -rw-rw-r--@  1 lizhongsu  staff       264 Apr 15 15:10 config.toml
    0 drwxrwxr-x@  9 lizhongsu  staff       288 Oct 10  2020 db
    0 drwxrwxr-x@  5 lizhongsu  staff       160 Jan 12 17:24 docs
   96 -rw-r--r--   1 lizhongsu  staff     49152 Feb 24 16:40 easyrtc.db
    8 -rw-rw-r--@  1 lizhongsu  staff       871 Oct 10  2020 go.mod
   88 -rw-rw-r--@  1 lizhongsu  staff     42294 Oct 10  2020 go.sum
    8 -rw-rw-r--@  1 lizhongsu  staff       846 Oct 10  2020 main.go
    8 -rw-rw-r--@  1 lizhongsu  staff       357 Oct 10  2020 package.json
    0 drwxrwxr-x@  5 lizhongsu  staff       160 Oct 10  2020 routes
    0 drwxrwxr-x@ 11 lizhongsu  staff       352 Jan 12 18:27 server
    0 drwxrwxr-x@  4 lizhongsu  staff       128 Oct 10  2020 util
    0 drwxrwxr-x@ 30 lizhongsu  staff       960 Apr 15 15:58 web_src

among adminiserver Is to generate files( linux)
2021 April 17