Monday, 4 September 2017

RPi cross-compiler

If we will be doing bare metal programming we need a cross-compiler.  Usually, when you compile a program you are expecting to execute it on the same machine or same type of system.  If I compile a C program on Windows I expect it will run on my Windows computer or other Windows computers but not on a linux system or a Mac.  A compiler which can generate programs for another system is a cross-compiler.
A compiler has to look at a program and translate it.  This is done in (at least) two stages, the first compiles the program into assembly language and the second assembles it into machine language.  For example a program snippet:
     if (letter='Z')
      {position=26;}
could compile to something like:   which is then assembled to machine code binary:
  load R1,'Z'                                          111100011111100011
  load R2,letter                                      
111100101111100011
  compare R1,R2                                   
0000110010101010
  branch-not-zero label$                       0001110000000000
  load R1,26                                          
111100101111100011
  store position, R1                                01110000001101010
 label$
The assembly code is specific to a system type and versions for Windows and Mac would have different instructions.  A new compiler version has to be written for each system.  Typically the assembler converts each line of code into a binary machine instruction so that a binary file can be loaded into the target system.  A cross-compiler needs to be told what the target system it is so that it can create appropriate binary files.

In addition to a cross-compiler we need some extra utilities to run programs a linker (to join the program with appropriate library routines) a loader (to copy the program on to the target system) and others.

Windows and linux systems are rather different so a Windows cross-compiler toolchain which generates RPi code is not terribly straightforward.  The easiest way to compile bare metal programs was to compile them on a RPi running linux and run them on another.
The  GNU (open source) C compiler gcc provides the necessary facilities to cross-compile so what we need is an environment to run gcc and the appropriate Raspberry pi definitions.
There are various options for compiling under Windows and I will look at some of them.  The main ones cygwin and mingw provide a linux environment allowing gcc to be used but there are others which I will try.

No comments:

Post a Comment