Linux Toolchains for Cross-Compilation

Cross-compile software using our pre-built toolchains for Linux for a variety of architectures.

This page provides toolchains for cross-compiling C and C++ code for Linux-based systems. The toolchains are pre-built and ready to use, allowing you to compile code on your host machine and run it on a target device with a different architecture.

Download the appropriate toolchain by selecting your host architecture (the architecture of the machine doing the build), target architecture (the architecture where the code will be executed), and libc vendor (glibc or musl).

Download and install the toolchain

  1. Download

    Package ID
    -
    Version
    -
    Filename
    -
    Checksum (SHA-256)
    -
  2. Install

    Pre-requisites

    Before using the Yocto SDK, you need python3, xz, and file utilities to be installed. For Ubuntu/Debian, install these tools with:

    apt update && apt install -y python3 xz-utils file

    SDK Installation

    Extract the SDK tarball:

    tar xvf path-to-sdk.tar.gz

    Then run the provided installer script on your machine to install the SDK:

    sh path-to-sdk-installer.sh

    During installation, you can choose the installation directory or use the default one (/opt/poky/${yocto-version} ).

    Setting up the environment

    After installing the SDK, you need to source the environment setup script on each terminal session where you want to use the SDK:

    . /path-to-sdk/path-to-environment-setup-script

    This script sets up all necessary environment variables for cross-compilation such as CC, CXX, LD, CFLAGS, CXXFLAGS, LDFLAGS, etc ... and configures build environments like Autotools.

    A CMake toolchain file is also included and can be found at the path specified by the CMAKE_TOOLCHAIN_FILE environment variable.

Terminology

A toolchain is a set of software tools used in a sequence to create other software, typically compilers, assemblers, and linkers.

Toolchains are created using the Yocto Project and are pre-built for your convenience. Yocto is an open-source collaboration project that provides templates, tools, and methods to help you create custom Linux-based systems for embedded products.

musl allows for static linking with libc/libc++ to create standalone binaries!

Motivation

As a developer deeply involved in C++ projects, I've encountered numerous distribution challenges that come with creating standalone binaries. After successfully navigating these complexities using Linux toolchains in combination with musl, I decided to share these powerful tools with everyone. This initiative aims to provide developers with reliable tools to compile and distribute C/C++ applications across various platforms, thus simplifying the software distribution process.

Architectures

We support a wide array of architectures for enhanced compatibility. For Intel 64-bit systems, our toolchains include multiple microarchitecture levels, notably x86_64_v2, the most prevalent today. For more details, see Intel 64-bit Microarchitecture Levels. Additionally, we support Intel 32-bit architectures such as x86_i686 and x86_core2. For ARM systems, we include both soft-float (armv6, armv7) and hard float (armv6hf, armv7hf) versions, as well as arm64 for 64-bit ARM architectures. Our support extends to MIPS and PPC architectures in both big and little endian configurations, along with more modern options like riscv64.

Technical Details

Our toolchains, crafted using the Yocto Project, include essential components such as GCC, make, cmake, binutils, gdb, python3, and perl. Each toolchain is equipped to handle the requirements for compiling C and C++ code. We provide specific versions of these components, as listed below:

Host Components

gcc13.3.0
gdb14.2
binutils2.42
cmake3.28.3
perl5.38.2
python33.12.3

Target Components

glibc2.39+git
libgcc13.3.0
libstdc++13.3.0
linux-libc-headers6.6
util-linux2.39.3
musl1.2.4+git0+79bdacff83

Hello World example in C++

Create a simple Hello World program in C++ and build it using the toolchain. The program will be statically linked with the C standard library and can be run on the target hardware.

Step 1: Install the toolchain on your machine

Install the toolchain on your machine by following the steps in the previous section. Use musl as the C standard library for static linking.

Step 2: Create the source files

Create a new directory for your project and navigate into it:

mkdir hello-worldcd hello-world

Create a new C++ file named main.cpp:

#include <iostream>

int main()
{
std::cout << "Hello, World!" << std::endl;
return 0;
}

Create a CMakeLists.txt file in the same directory:

cmake_minimum_required(VERSION 3.28)
project(hello-world LANGUAGES CXX)
# Set C++ standard
set(CMAKE_CXX_STANDARD 20)
# Add executable
add_executable(hello-world main.cpp)
# Ensure static linking
set(CMAKE_EXE_LINKER_FLAGS "-static -static-libgcc -static-libstdc++")

This CMake file sets the project name, specifies the required C++ standard, and adds an executable target built from main.cpp. It also sets the linker flags to ensure static linking.

Step 4: Build the project using the toolchain

Source the environment setup script to configure the build environment:

. /path-to-sdk/path-to-environment-setup-script

Create a build directory and navigate into it:

mkdir buildcd build

Run CMake to configure the build environment using the CMake toolchain file specified by the environment variable CMAKE_TOOLCHAIN_FILE. This variable is set during the SDK setup and points to the appropriate toolchain file.

cmake .. -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} -DCMAKE_VERBOSE_MAKEFILE=TRUE

Build the project:

make

The build process generates an executable named hello-world in the build directory. The binary is statically linked with the C standard library and can be run on the target hardware.

Step 5: Ensure the binary is statically linked

Check the binary for dynamic dependencies using the ldd command:

ldd hello-world

The output should indicate that the binary is statically linked and has no dynamic dependencies.

Would you like to support our work?

Your contribution helps us continue creating great content.

Frequently asked questions

Can’t find the answer you’re looking for? Reach out to our customer support.

A toolchain is a set of software tools used in sequence to create other software. It typically includes compilers, assemblers, and linkers. Toolchains are essential for building, compiling, and linking code, enabling you to create executable programs for different target architectures.

The host architecture is the architecture of the machine performing the build process (e.g., x86_64 or aarch64). The target architecture is the architecture on which the built code will be executed (e.g., x86_64, arm64, etc.).

Glibc (GNU C Library) and musl are both implementations of the standard C library, which provides essential APIs for C programs. Glibc is widely used and well-supported, making it a common choice for many Linux distributions. musl is designed to be lightweight and efficient, allowing for static linking to create standalone binaries. A standalone binary includes all necessary libraries within the executable, enabling it to run on any compatible system without needing external libraries.

Yes, you can use these toolchains for commercial projects. The toolchains are provided without any commercial licenses, so you are free to use them in both personal and commercial applications.