Skip to content

Cross-compilation on Ubuntu

Setting up a cross-compilation environment for ARM and PowerPC on an x86 Ubuntu system is typically done by installing pre-built cross-compiler toolchains from the Ubuntu repositories. This is the recommended approach for standard Linux targets.


Installing Cross-Compilers for ARM and PowerPC

Section titled “Installing Cross-Compilers for ARM and PowerPC”

You can install the necessary GCC cross-compilers and their associated binary utilities (binutils) directly using the apt package manager.

First, ensure your package list is up-to-date:

Terminal window
sudo apt update

For ARM (32-bit and 64-bit Linux targets), you’ll install the following packages:

ArchitectureTarget TripleCompiler PackageCommand
ARM 32-bit (Hard-Float)arm-linux-gnueabihfgcc-arm-linux-gnueabihfsudo apt install gcc-arm-linux-gnueabihf
ARM 64-bit (AArch64)aarch64-linux-gnugcc-aarch64-linux-gnusudo apt install gcc-aarch64-linux-gnu

You can install both with one command:

Terminal window
sudo apt install gcc-arm-linux-gnueabihf gcc-aarch64-linux-gnu

For PowerPC (32-bit and 64-bit Linux targets):

ArchitectureTarget TripleCompiler PackageCommand
PowerPC 32-bitpowerpc-linux-gnugcc-powerpc-linux-gnusudo apt install gcc-powerpc-linux-gnu
PowerPC 64-bit (Little-Endian)powerpc64le-linux-gnugcc-powerpc64le-linux-gnusudo apt install gcc-powerpc64le-linux-gnu

Install them using:

Terminal window
sudo apt install gcc-powerpc-linux-gnu gcc-powerpc64le-linux-gnu

Once installed, the cross-compilers and tools will have a prefix corresponding to their target triple. You’ll use these specific compiler names instead of the default gcc or g++.

For a source file named hello.c:

Target ArchitectureCompiler Command
ARM 32-bitarm-linux-gnueabihf-gcc hello.c -o hello_arm32
ARM 64-bitaarch64-linux-gnu-gcc hello.c -o hello_arm64
PowerPC 32-bitpowerpc-linux-gnu-gcc hello.c -o hello_ppc32
PowerPC 64-bitpowerpc64le-linux-gnu-gcc hello.c -o hello_ppc64

You can verify the output file type using the file command:

Terminal window
file hello_arm64
# Expected output: ...ELF 64-bit LSB executable, **ARM aarch64**, version 1 (SYSV)...

Compiling Universal (Cross-Platform Library)

Section titled “Compiling Universal (Cross-Platform Library)”

When compiling a project like your Universal library, you’ll need to pass the appropriate compiler and configuration options to its build system (e.g., Make, CMake, Autotools).

Set the cross-compiler for C, C++, and specify the host architecture using the --host flag:

Terminal window
# Example for ARM 64-bit
export CC=aarch64-linux-gnu-gcc
export CXX=aarch64-linux-gnu-g++
./configure --host=aarch64-linux-gnu
make

The recommended way for CMake is to use a Toolchain File. This file, often named Toolchain-<ARCH>.cmake, tells CMake about the compilers and system roots for the target.

Example Toolchain-arm64.cmake:

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
# Specify the cross compilers
set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)
# Specify the sysroot (where target headers/libraries are)
# On Ubuntu, this is often the default path for the installed toolchain
# set(CMAKE_FIND_ROOT_PATH /usr/aarch64-linux-gnu)
# set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
# set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

Then, configure your build:

Terminal window
mkdir build_arm64
cd build_arm64
cmake -DCMAKE_TOOLCHAIN_FILE=../Toolchain-arm64.cmake ..
make