Solutions to exercise 4: The Raspberry Pi

Most of the exercises refer to exercises we have done in the sessions before on the PC and there are therefore no further solutions. The only thing new is cross-compilation and I want to use the "hello world" example as a demonstration:
  • First we compile the program on the PC with a standard Makefile as we had done before
  • Then we check with the file command the type of executable that has been created and we execute the program on the PC
  • The executable is then transferred to the Pi (the host name of my Pi is "ttn-gateway"
  • Trying to execute this PC executable fails
  • Using the cross-compiler in Makefile.pi creates an executable for the ARM machine
  • After transfer to the Pi we can execute the program normally.
  • Trying to execute the ARM program on my PC also works! Why?
crossCompile.png

In the screen dump above I first show you the Makefile for native and for cross compilation. Please note that the only difference is the line
CC=arm-linux-gnueabihf-gcc

If I compile with make (using Makefile) I create an executable for the PC (hello: ELF 64-bit LSB shared object, x86-64,...). When transferring this file to the Pi with scp and running it there we see that it fails:

PChelloOnPi.png

When however we do the same thing with the command make -f Makefile.pi (using Makefile.pi instead) we get an executable for the ARM (hello: ELF 32-bit LSB executable, ARM, EABI5 version ...) which works perfectly fine on the Pi:

helloPi.png

It looks very strange that the ARM executable also works on my PC! The reason is that I have qemu, the ARM emulator installed on my PC. In this case the emulator takes the binary ARM code and interprets it using X86 instructions. When you try this on your PC it may work if qemu is correctly installed. There is a high probability however that you will get an error like this:

libNotFound.png

The problem is that qemu does not find the shared libraries needed to run the program. I fixed this defining the environment variable QEMU_LD_PREFIX which points to the directory where the shared libraries are found.

Comments

 
I Attachment Action Size Date Who Comment
PChelloOnPi.pngpng PChelloOnPi.png manage 36 K 30 Oct 2018 - 10:56 UliRaich  
crossCompile.pngpng crossCompile.png manage 161 K 30 Oct 2018 - 10:59 UliRaich  
helloPi.pngpng helloPi.png manage 37 K 30 Oct 2018 - 10:56 UliRaich  
libNotFound.pngpng libNotFound.png manage 16 K 30 Oct 2018 - 11:13 UliRaich  
Topic revision: r1 - 30 Oct 2018, UliRaich