EBC Cross-Compiling

This class is about developing software for embedded Linux. So far we have been doing all of our development on BeagleBone Black. This works well for small (and not so small) programs. In EBC_Installing_Kernel_Source the scripts automatically installed the cross-compiler and tools needed for cross development in ~/BeagleBoard/bb-kernel/dl/gcc-linaro-4.9-2015.02-3-x86_64_arm-linux-gnueabihf/bin, but it's tricky to easily use those tools. Rather here we'll use the package manager to install the tools in a standard path.

Cross-compiling Hello World
This shows how to cross-compile with compiler loaded with

host$ sudo apt-get install gcc-arm-linux-gnueabihf

Listing 2-4 on page 29 of the text is an embedded version of Hello World. If you've set up your git repository you will find it in helloWorld.c when you do a git pull. Compile and run it on your host to be sure it works.

host$ gcc helloWorld.c host$ file a.out a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=a690343a40944e2d43cdf7314e4b8bdd298ed464, not stripped host$ ./a.out Hello, World! Main is executing at 0x400524 This address (0x7fff8260bdf8) is in our stack frame This address (0x601038) is in our bss section This address (0x601020) is in our data section

Now that you know it's working, let's cross compile it. First set the paths to find the cross-compiler. Put the following in a file, call it ~/crossCompileEnv.sh.

export ARCH=arm export CROSS_COMPILE=arm-linux-gnueabihf-

Now source the file and compile again. (Note: you only have to source once per terminal session.) host$ source ~/crossCompileEnv.sh host$ ${CROSS_COMPILE}gcc helloWorld.c host$ file a.out a.out: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.31, BuildID[sha1]=0x10182364352b9f3cb15d1aa61395aeede11a52ad, not stripped

The file command tells what's in the file. In this case we have an ARM executable. Success! Now copy to your Beagle and run

host$ scp a.out root@192.168.7.2:. host$ ssh root@192.168.7.2 ./a.out Hello, World! Main is executing at 0x8374 This address (0xbeb32d4c) is in our stack frame This address (0x10650) is in our bss section This address (0x10648) is in our data section

The scp copies a.out to the beagle and the ssh runs the a.out on the beagle. Notice the addresses are very different from the host version.