How to Use GNU ARM Toolchain: Syntax, Example, and Tips
To use the
GNU ARM toolchain, you compile your ARM source code with arm-none-eabi-gcc, assemble with arm-none-eabi-as, and link with arm-none-eabi-ld. Then, use arm-none-eabi-gdb to debug your program on ARM devices or emulators.Syntax
The GNU ARM toolchain uses several commands for compiling, assembling, linking, and debugging ARM programs:
- arm-none-eabi-gcc: Compiles C/C++ source files to ARM object files.
- arm-none-eabi-as: Assembles ARM assembly code into object files.
- arm-none-eabi-ld: Links object files into a final executable or binary.
- arm-none-eabi-gdb: Debugs ARM executables on hardware or simulators.
Basic compile and link syntax:
arm-none-eabi-gcc [options] source.c -o output.elf
To assemble:
arm-none-eabi-as source.s -o output.o
To link:
arm-none-eabi-ld input.o -o output.elf
bash
arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -O2 -g source.c -o output.elf
Example
This example compiles a simple ARM C program that toggles an LED (conceptual) and produces an executable for an ARM Cortex-M4 processor.
c
#include <stdint.h> volatile uint32_t *GPIO_PORT = (uint32_t *)0x40021018; // Example GPIO port address int main() { while (1) { *GPIO_PORT ^= 0x1; // Toggle LED bit for (volatile int i = 0; i < 100000; i++); // Delay loop } return 0; } // Compile command: // arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -O2 -g led_toggle.c -o led_toggle.elf
Common Pitfalls
Common mistakes when using the GNU ARM toolchain include:
- Not specifying the correct CPU architecture with
-mcpuor-march, causing incompatible code. - Forgetting
-mthumbfor Thumb instruction set, which most ARM microcontrollers use. - Missing linker scripts, which define memory layout; without them, linking fails or produces unusable binaries.
- Using standard
gccinstead ofarm-none-eabi-gccfor ARM targets.
Wrong:
gcc source.c -o output.elf
Right:
arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb source.c -o output.elf
bash
gcc source.c -o output.elf arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb source.c -o output.elf
Quick Reference
| Command | Purpose | Common Options |
|---|---|---|
| arm-none-eabi-gcc | Compile C/C++ to ARM object or executable | -mcpu= |
| arm-none-eabi-as | Assemble ARM assembly code | -mcpu= |
| arm-none-eabi-ld | Link object files | -T |
| arm-none-eabi-gdb | Debug ARM executables | --eval-command= |
Key Takeaways
Always use arm-none-eabi-gcc to compile for ARM targets, not regular gcc.
Specify the correct CPU and Thumb mode with -mcpu and -mthumb options.
Use a proper linker script to define memory layout for your ARM device.
Debug ARM programs with arm-none-eabi-gdb on hardware or simulators.
Check for common mistakes like missing options or wrong tool usage.