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.
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.