SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: How to Install GCC for ARM
SUBSCRIBE
SoCSoC
Font ResizerAa
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Search
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Have an existing account? Sign In
Follow US
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
© S-O-C.ORG, All Rights Reserved.
Arm

How to Install GCC for ARM

Eileen David
Last updated: September 12, 2023 7:49 am
Eileen David 4 Min Read
Share
SHARE

The GNU Compiler Collection (GCC) is a widely used compiler system that supports multiple programming languages including C, C++, Objective-C, and more. GCC can generate code for a variety of processor architectures including ARM. Installing GCC for ARM allows you to compile code specifically for ARM-based systems.

Contents
1. Install Build Essentials2. Install ARM Cross Compilation Tools3. Set Environment Variables4. Install GCC for ARM5. Create a Test C Program6. Cross Compile the Program7. Install QEMU User Emulation8. Run the ARM Binary9. ARM Hardware Testing10. C++ and Other LanguagesConclusion

Here is a step-by-step guide on how to install GCC for ARM on Linux:

1. Install Build Essentials

GCC and other compilation tools rely on various libraries and utilities like make, binutils, and more. We can install these dependencies in one go on Debian/Ubuntu systems using:

sudo apt update
sudo apt install build-essential

On RHEL/CentOS systems, we’d use:

sudo yum groupinstall 'Development Tools'

2. Install ARM Cross Compilation Tools

To compile code for ARM, we need the ARM cross compiler and other utilities. On Debian/Ubuntu:

sudo apt install crossbuild-essential-armhf

On RHEL/CentOS:

sudo yum install arm-linux-gnu-gcc arm-linux-gnu-binutils arm-linux-gnu-glibc

This will install the ARM cross compiler tools like arm-linux-gnu-gcc, arm-linux-gnu-objdump etc.

3. Set Environment Variables

We need to set some environment variables for the cross compiler to work properly:

export CROSS_COMPILE=arm-linux-gnu-
export PATH=$PATH:/usr/arm-linux-gnu/bin

CROSS_COMPILE tells GCC the prefix for our cross compiler tools. PATH adds the ARM toolchain binaries to the path.

4. Install GCC for ARM

Now we can install the ARM version of GCC itself. On Debian/Ubuntu:

sudo apt install gcc-arm-linux-gnueabihf

On RHEL/CentOS:

sudo yum install arm-linux-gnueabihf-gcc

This will install gcc-arm-linux-gnueabihf or arm-linux-gnueabihf-gcc respectively.

5. Create a Test C Program

Let’s create a simple “hello world” test program:

nano hello.c

With the following contents:

#include <stdio.h>

int main() {
  printf("Hello World!");
  return 0;
}

6. Cross Compile the Program

Now we can cross compile hello.c for ARM using the -march flag:

arm-linux-gnueabihf-gcc -march=armv7-a hello.c -o hello

This will generate the executable hello compiled for ARM. We can verify it using:

file hello

Which should output something like:

hello: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 4.4.0, BuildID[sha1]=ada4c1c319792b6d0e2f498f4a90b700d8d96a8d, with debug_info, not stripped

Confirming this is a 32-bit ARM executable.

7. Install QEMU User Emulation

To test the compiled ARM code on our x86 development system, we can use QEMU for user emulation:

sudo apt install qemu-user
# or 
sudo yum install qemu-user-static

8. Run the ARM Binary

We can now execute the ARM binary using:

qemu-arm hello

This will emulate the ARM environment and run our hello executable, outputting:

Hello World!

Confirming that GCC can successfully cross compile C programs for ARM.

9. ARM Hardware Testing

For testing on physical ARM hardware, we would copy the executable over and run it directly. For example on Raspberry Pi:

scp hello pi@raspberrypi:/home/pi
ssh pi@raspberrypi
./hello

This copies over and runs the ARM executable on the Pi itself.

10. C++ and Other Languages

GCC can compile other languages like C++ for ARM as well using g++. The process is the same, we just use g++ instead of gcc:

arm-linux-gnueabihf-g++ -march=armv7-a hello.cpp -o hello

Objective-C, Go, Fortran etc can also be cross compiled this way.

Conclusion

Installing GCC for ARM allows compiling code specifically for the ARM architecture. This allows creating ARM executables on x86 systems for testing in emulators before deploying onto ARM devices.

The process involves installing cross compiler tools, setting environment variables, installing the ARM GCC version, and using flags like -march to generate binaries for the target ARM platform.

Cross compiling with GCC is crucial for ARM development workflows. This guide covered the core steps of getting GCC setup for ARM on Linux systems.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article Does GCC work on ARM?
Next Article What is a GPIO register?
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

2k Followers Like
3k Followers Follow
10.1k Followers Pin
- Sponsored-
Ad image

You Might Also Like

What are the purposes of the ARM ABI and EABI?

The ARM Application Binary Interface (ABI) and Embedded ABI (EABI)…

6 Min Read

ARM Cortex-M0 SPI outputting 16bit not 8bit

When configuring the SPI (Serial Peripheral Interface) on an ARM…

8 Min Read

What is the TM4C123 Microcontroller?

The TM4C123 is a 32-bit ARM Cortex-M4 based microcontroller from…

9 Min Read

Faster way of multiplying 2 32-bit numbers giving a 64-bit result in Cortex M0/M0+

Multiplying two 32-bit numbers to get a 64-bit result is…

9 Min Read
SoCSoC
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
Welcome Back!

Sign in to your account