A technical blog about Embedded, Coding, IoT, Autosar and AI.

Home/Embedded/Embedded Linux/[Embedded Linux - 005] Understanding Cross Compilation and Toolchains in Embedded Systems
EmbeddedEmbedded LinuxAuthor: CharilenVuMarkdown

[Embedded Linux - 005] Understanding Cross Compilation and Toolchains in Embedded Systems

Learn the fundamentals of cross-compilation and toolchains in embedded systems, including the concepts of host and target machines and the key components used to build embedded software.

Overview

On This Page

Cross Compilation and Toolchains

Introduction

Modern software development relies on a set of tools that transform source code into executable programs. These tools are typically organized into a toolchain, where each tool performs a specific stage in the build pipeline.

In many cases, the machine used for development is different from the machine where the program will run. This is common in embedded systems, IoT devices, and low-resource platforms. To support this workflow, developers use cross compilation.

This article explains:

  • What a toolchain is
  • The GNU toolchain
  • How the compilation pipeline works
  • Libraries and linking
  • Cross compilation concepts
  • Toolchain structure and sysroot

What is a Toolchain?

A toolchain is a collection of software development tools used to transform source code into executable programs.

Each tool performs a specific stage in the software development pipeline.

A typical toolchain includes:

  • A compiler that converts source code into machine code
  • An assembler that converts assembly code into object files
  • A linker that combines object files into an executable
  • Libraries that provide reusable functionality
  • A debugger used to inspect and debug programs
  • Build tools that automate the compilation process

Components of a Toolchain

ToolDescription
Compiler (gcc / clang)Converts source code into machine code
Assembler (as)Converts assembly code into object files
Linker (ld)Combines object files into a final executable
Standard libraries (glibc / musl)Provide runtime functionality
Debugger (gdb)Helps debug programs
Build tools (make / cmake)Automate the compilation process

Example of common tools in the GNU toolchain:

gcc g++ ld as gdb make libc

The GNU Toolchain

The GNU Toolchain is a collection of development tools produced by the GNU Project.

These tools form a powerful environment used for building:

  • Linux systems
  • BSD operating systems
  • Embedded systems
  • Software applications The GNU toolchain plays a vital role in modern system software development.

The Compilation Process

Compiling a program is not a single step. It is a multi-stage pipline tha transforms source code into an executable program. The typical compilation process looks like this:

  Source Code (.c)
      │
      ▼
  Preprocessor
      │ preprocesed file (.i file) 
      ▼
  Compilation
      │ assembly code (.s file)
      ▼
  Assembly
      │ object code (.o file)
      ▼
   Linker
      │
      ▼
  Executable

What is Cross Compilation?

Cross Compilation is the process of compiling source code on one platform (called to host) to procedure excecutable code for a different platform (called the target). This is common in embedded systems where the target device has limited resources.

Basic concept

bash
Host Machine (x86 PC)
        |
        |  Cross Compiler
        v
Target Binary (ARM / RISC-V / MCU)

The host machine runs the compliler, while the resulting binary is intended to run on the target hardware.

Example

bash
arm-linux-gnueabihf-gcc hello.c -o hello

This command compiles hello.c into an executable for ARM architecture, even though the compilation happens on an x86 machine.

Build, Host, and Target Systems

When working with cross compilation, three important system definitions are used.

TermDescription
BuildThe system where the toolchain itself is compiled
HostThe system where the compiler runs
TargetThe system where the compiled program runs

Example configuration:

bash
Build  : x86_64 Linux  
Host   : x86_64 Linux  
Target : ARM Linux

This means the compiler runs on an x86 system, but produces programs that run on ARM devices.

What is a Cross Toolchain?

A cross toolchain is a toolchain tha procedures binaries for a different architecture then the machine running the compiler. For example, a developer using an x86 computer may need to build programs that run on an ARM embeeded board Common cross-compliers include:

ToolchainTarget Architecture
arm-linux-gnueabihf-gccARM (32-bit)
aarch64-linux-gnu-gccARM64
riscv64-linux-gnu-gccRISC-V

Example

bash
arm-linux-gnueabihf-gcc hello.c -o hello

Why Cross Compilation is important?

Many target systems are not capable of compling software directly. Reasons include limited resources such as:

DeviceLimitation
MicrocontrollersNo operating system or compiler support
Embedded boardsLimited CPU performance
RoutersVery small memory capacity
IoT devicesRestricted storage and computing power

Because of these limitations, developers usually compile software on a powerfull host machine and then transfer the resulting binaries to the target device.

Toolchain Structure

A typical cross toolchain has the following structure:

toolchain/
 ├── bin/
 │    ├── arm-linux-gnueabihf-gcc
 │    ├── arm-linux-gnueabihf-ld
 │
 ├── lib/
 │
 ├── include/
 │
 └── sysroot/
      ├── usr/
      ├── lib/
      └── include/

Sysroot

A sysroot represents the root filesystem of the target system during compilation.

It contains:

  • Target libraries
  • Target headers
  • Target filesystem layout

Example: /opt/toolchain/sysroot

The compiler uses this directory to locate libraries and headers for the target architecture.

Typical Cross Compilation Workflow

A common development workflow in embedded systems looks like this:

  Developer PC
      │
      │ Cross Toolchain
      ▼
Compile Applications
      │
      ▼
 Target Binary
      │
      ▼
Deploy to Device
      │
      ▼
Run on Target System

Example workflow

1. Install cross compiler

bash
sudo apt install gcc-arm-linux-gnueabihf

2. Compile the program

bash
arm-linux-gnueabihf-gcc test.c -o test

3. Transfer the binary to the device

bash
scp test pi@raspberrypi:/home/pi/

4. Run the program on the target system

bash
./test

This workflow allows developers to develop and build software on a PC while running it on a different architecture device.

Summary

ConceptDescription
ToolchainSet of tools used to build software
GNU ToolchainWidely used development toolchain
Compilation pipelineSteps transforming source code into executables
LibrariesReusable code modules
Cross compilationBuilding software for another architecture
Cross toolchainToolchain configured for a different CPU
SysrootTarget filesystem used during compilation

Cross compilation and toolchains are fundamental concepts in embedded systems development, operating system engineering, and low-level software development.

Image

Continue Reading

Previous and Next Articles

Related Articles

Community

Leave a Comment

Loading comments...
Loading comments...