You can perform cross compile CGO and get binary in GitHub Action

Photo by Richy Great on Unsplash

Go allows you to cross-compile executable binary natively. However, it require more complicated when using CGO to compile binary since you also required the related cross-compiler which not every OS available, especially for Windows.

As a developer, multi-platform support is a headache problem. Especially when target to Apple products, but you can’t afford the price of the products and just throwing “How to build” for those who using Mac including running on Apple Silicon natively.

For the naïve developer, building by their hand is not a easy task even how do you provided building script like Makefile. And reported error either TL;DR or too few to tell like this:

This error make them to give up using the software which required compile by themselves with this reaction:

Same reaction when build failed and can not find the problem

Let handle it by running in GitHub Actions

It is no doubt that only creators know how binary build exactly. But performing CGO cross-compiling require specific software to generate the binary code is running on targeted architecture.

Photo by Jainath Ponnala on Unsplash

For an example, if you want to build a dynamic library namely “foo” for Raspberry Pi 4 on 64-bit platform but your Pi is no longer worked. You can parse compiling task to GitHub Actions with following this configurations:

(This file is for reference only)

In this GitHub Actions YAML configurations files, these configurations is mandatory which reference with this issue:

  • Cross-compiler (aarch64-linux-gnu-gcc) and you must install this by calling “apt-get”
  • Go actions package
  • CGO_ENABLED and targeted GOARCH environment variable
  • Upload artifact for getting compiled files

The exported file is packed inside a zip file already which no additional archive process needed. Instead, if you want to get the compiled binary only, please set target location in a directory (“bin”) to prevent waste bandwidth for downloading unnecessary files.

It can be used for compiling CGO binary for Apple Silicon with built-in XCode helps

Photo by Domenico Loia on Unsplash

In fact, you can compile CGO for macOS with GitHub Actions once you know how to perform cross-compile on Linux which make a good concept of the workflow. With including macOS 11 Big Sur SDK in macOS platform of the Actions, it contains a cross-compiler for Apple Silicon. But according to the list of installed software, it does not mentioned the exact path of the XCode SDK which make more unconfirmed for those who never uses macOS.

In summary

With GitHub Actions helps, you can compile CGO to what platform do you want with proper cross-compiler for generating the binary file that ready to uses. Thus, it does not charge additional fee if using in public repository which reduce the cost of setup develop environment for specific platform (including power usage, Internet and etc…).

Despite it make release more quicker with just one web platforms, it still required how many experiences on targeted OS. For me, I never uses macOS and I have no idea where macOS SDK is stored. However, since I have well experience on Ubuntu, I can post the example of cross-compiling Linux from AMD64 to ARM64 with giving Raspberry Pi as example.

Official rk0cc Medium account. With pushing opinion of my viewpoint of IT and gaming related topic