Tuesday, March 29, 2016

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

This is the final of 5 posts on setting up and using a PC cross compiler to make Raspberry Pi programs.

Following the previous posts, we have the Raspberry Pi compatible gcc compiler on our PC with library files copied over from the current Raspbian Jessie. We also have an updated Pi ready to run programs.
The make utility is often used by professionals to pull together a collection of files to build a program and there is a copy of a compatible Windows Make utility in C:\SysGCC\bin.  But most Raspberry pi programs are not that complicated!  We can use gcc to make programs with only one source file. Let's try it out. Type or copy to the file HelloWorld.c in Notepad, Wordpad (text), or other text editor:

#include <stdio.h>

int main()
    printf("Hello, world\n");

If it looks different than an Arduino sketch, it is. It is standard C, straight out of Kernighan and Ritchie. Place the file HelloWorld.c into a fresh program directory. The following Windows Command ("Batch") file helps automate the compile process - copy this to another text file called gcc.bat:

C:\SysGCC\bin\arm-linux-gnueabihf-g++.exe %1.c -o %1
if errorlevel 1 (
   ECHO Compile Fail - error %errorlevel%
   exit /b %errorlevel%
  ECHO Must enter parameter like "C:\> gcc myfile" (no .c needed)

If you installed the compiler into a different directory than C:\SysGCC, change the second and fourth lines to match.  All of this doesn't do anything fancy - it uses some command code (like a Unix shell script) to make sure you feed it a file name and that the compile worked (or not).

If you are in Windows File Explorer in the directory of your program, you'll see HelloWorld.c and gcc.bat. We will use a command line prompt to run the compiler (since it is a command program, not a graphical program). While holding the PC Shift key on the keyboard, right click the mouse in the Explorer window. Select the entry Open Command Window Here. You will have a Windows command window open in your program directory, which you can verify by typing the dir command. Compile your program by typing gcc <programname> - for our test, we'll type gcc HelloWorld (no .c should be typed).

With no error messages, your program worked!  You now have a file named HelloWorld (no extension) in the directory.  This is your Pi program.

Transferring Your Program to Your Pi

The binary executable file now needs to go from the PC to the Pi. This may be done in multiple ways but the simplest way is using the Pi Finder utility we used earlier.

Press the Upload button and select your HelloWorld file we just made in your project directory (do not select the HelloWorld.c file.  If you don't have a HelloWorld file, around 6,500 bytes, go back to compile it. When you complete the upload process, the file is in your ~/home/pi directory:

You need to execute the command chmod +x HelloWorld to tell the Pi the file is an executable (and not a data file). The file name will be in green when you type the file listing (directory) command ls.

Now you can execute your program (finally!) by typing ./HelloWorld (the preceding ./ tells Raspbian Linux we're executing the program in the current directory).  It prints Hello, world - success!

Going Further

Now we can build more complex programs, perhaps Arduino-like programs.  We'll explore these in coming blog posts.  Thanks for following this 5 post series and as always leave comments if you like, reshare with your colleagues & friends.

Other blog posts in this series:
  1. Introduction
  2. PC Setup
  3. Pi Setup
  4. Library Setup
  5. Use (this post)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.