Such Programming

Tinkerings and Ramblings

Cross Compiling C Code for ARM

Hello! In this brief post I will share how you can cross compile some simple C code for running on an ARM based device. I have a desire to write a few simple things in C for tinkering with OpenBMC on Barreleye G2 and figured I would share my process.

ad

The Build Box

To build this program I’m going to use a freshly built Ubuntu 16.04.3 VM, that way I know for sure what dependencies are needed. My host system is also running Ubuntu 16.04.3 and I’m using Virt Manager as an interface to libvirt that is serving my VMs via QEMU and KVM.

Starting the install

After a few minutes I am ready to go!

The Codes

The code for this is pretty simple, I’ll just build the ol’ hello world!

#include <stdio.h>
int main() {
    printf("Hello, ARM!\n");
    return 0;
}

I’ll SSH into my VM so that my commands are easier to follow. I start by creating the source file with vim.

Writing hello.c in VIM

Building and Testing

Ubuntu offers many pre-built cross compilers. All you need to do is run a sudo apt update to ensure your package listing is up to date and a sudo apt install -y build-essential gcc-arm-linux-gnueabi will get you what you need.

To build, I’ll use the arm-linux-gnueabi-gcc compiler instead of just gcc. I’ll verify it was indeed for an ARM machine with readelf.

Building and copying the binary

Next, to test it, I’ll scp it over to my OpenBMC system, ssh into it and give it a go!

Building and copying the binary

There it is! I’ve successfully cross compiled a simple program to run on an ARM machine.

ad