Monday, March 28, 2016

Cross Compiling - Compile C Programs on Your Windows Machine for a Raspberry Pi - Introduction

Cross Compiling is the process of making an executable program for one computer with the goal of running the program on another (different) computer. This can be very handy! Your desktop or laptop is probably more capable than your Raspberry Pi or other development device. Your PC has the edge with a better display, more memory, and speed.

With compiled programs like C and C++, this can be a more ideal procedure when compared to Python which is interpreted on the fly (thus no speed increase by doing compilation beforehand). Many people are familiar with C and C++ as the basis for the programming when using the Arduino development environment.

The diagram below shows how this would be accomplished.
The open source gcc compiler can generate code for many different computers. What we are looking for in this series is a Windows executable compiler which generates Raspberry Pi executables. There are many more combinations. Raspberry Pi cross compilers most often use Linux as the PC-side operating system and you can find Mac hosts as well.

Unfortunately, getting the cross compiler is not as simple as going to the Raspberry Pi website or GitHub repository (at present). The code for building the gcc toolchain (the programs that make up the cross compiler) is available but you need a native compiler (in this case Windows) and an environment that will take the source code and make the tools you want. Since gcc is a compiler that is Unix/Linux centric, you can set up an environment that helps Windows work more like *nix, the most common two being MinGW and Cygwin. This complicates the upfront work to set things up, something that we'd like to avoid at present.

The tutorial in following posts will demonstrate an effective cross compile process by using tools developed by SysProgs and Adafruit.

  1. This introduction
  2. PC Setup
  3. Pi Setup
  4. Library Setup
  5. Use