Cross Compiling on macOS via Docker

Dec 23, 2020

I needed to compile an arm64 binary to run on the UniFi Dream Machine Pro. This is how I used docker to setup cross compilation tools and compile the binary on macOS.

Get and install Docker Desktop is not already installed. Once installed we can use DockCross to setup an cross compile tool chain we want. Here is an example of how I setup the linux-arm64 toolchain.

mkdir -p ~/Documents/dockcross
cd ~/Documents/dockcross
docker run --rm dockcross/linux-arm64 > ./dockcross-linux-arm64
chmod +x ./dockcross-linux-arm64

This will setup a linux-arm64 cross compiler. To compile files using this compiler you have to place the files in the same directory as the binary. Here is an exaple of how to compile.

./dockcross-linux-arm64 bash \
-c '$CC -s -O3 main.c -o ./udp-broadcast-relay-redux'

Examining the compiled binary with file command should display ARM aarch64 architecture.

file udp-broadcast-relay-redux
udp-broadcast-relay-redux: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), 
dynamically linked, interpreter /lib/, for GNU/Linux 4.10.8, stripped

Note: Using $CC is important. If you use gcc directly it will compile using x86_64 version.

How TosProgrammingNetworkingDockerUbiquiti

macOS and Active Directory Passwords

Dagger 2 Beginner Tutorial