1. Technology

Hello World in C on the Raspberry Pi

By

Raspberry Pi Logo (see http://raspberrypi.org)

This set of instructions won't suit everybody but I'll try to be as generic as possible. I installed the Debian Squeeze distribution, so the programming tutorials are based on that. Initially, I'm starting off by compiling programs on the Raspi but given its relative slowness to any PC in the last ten years, it's probably best to switch to developing on another PC and copying the executables over.

I'll cover that in a future tutorial, but for now it's about compiling on the Raspi.

Preparing for Developing

The starting point is you have a Raspi with a working distribution. In my case it's Debian Squeeze which I burnt with instructions from the RPI Easy SD Card Setup. Make sure you bookmark the Wiki as it's got tons of useful stuff.

If your Raspi has booted and you've logged in (username pi, p/w = raspberry) then type gcc - v at the command line. You'll see something like this:

Using built-in specs.
Target: arm-linux-gnueabi
Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib
--enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --disable-sjlj-exceptions --enable-checking=release --build=arm-linux-gnueabi
--host=arm-linux-gnueabi --target=arm-linux-gnueabi
Thread model: posix
gcc version 4.4.5 (Debian 4.4.5-8)

Install Samba

One of the first things I did and recommend to you if you have a Windows PC on the same network as your Raspi is to install and setup Samba so you can access the Raspi. Then I issued this command:

gcc -v >& l.txt

To get the above listing into the file l.txt that I could view and copy on my Windows PC.

Even if you are compiling on the Raspi, you can edit source code from your Windows box and compile on the Raspi. You can't just compile on your Windows box using say MinGW unless your gcc is configured to output ARM code. That can be done but let's learn to walk first and learn how to compile and run programs on the Raspi.

GUI or Terminal

I'll assume that you are new to Linux, so apologies if you know it already. You can do most of the work from the Linux terminal (= command line). But it can be easier if you fire up the GUI (Graphical User Interface) to have a look around the file system. Type startx to do that.

The mouse cursor will appear and you can click in the bottom left hand corner (it looks like a mountain( to see the menus. Click on Accessories and run File Manager to let you view folders and files.

You can close it down any time and return to the terminal by clicking the little red button with a white circle in the bottom right hand corner. Then click on Logout to return to the command line.

You may prefer to have the GUI open all the time. When you want a terminal click the bottom left button then click Other on the menu and Terminal. In the Terminal you can close it by typing Exit or click the Windows like x in the top right hand corner.

Folders

The Samba instructions on the Wiki tell you how to setup a public folder. It's probably best to do that. Your home folder (pi) will be readonly and you want to write to the public folder. I created a sub-folder in public called code and created the hello.c file listed below in it from my Windows PC.

If you prefer to edit on the PI, it comes with a text editor called Nano. You can run it from the GUI on the other menu or from the terminal by typing

sudo nano
sudo nano hello.c

The sudo elevates nano so it can write files with root access. You can run it just as nano, but in some folders that won't give you write access and you won't be able to save files so running things with sudo is usually best.

Hello World

Here's the code:

#include <stdio.h>

int main() {
    printf("Hello World\n");
    return 0;
}

Now type in gcc -o hello hello.c and it will compile in a second or two.

Take a look at the files in the terminal by typing in ls -al and you'll see a file listing like this:

drwxrwx--x 2 pi users 4096 Jun 22 22:19 .
drwxrwxr-x 3 root users 4096 Jun 22 22:05 ..
-rwxr-xr-x 1 pi pi 5163 Jun 22 22:15 hello
-rw-rw---- 1 pi users 78 Jun 22 22:16 hello.c

and type in ./hello to execute the compiled program and see Hello World.

That completes the first of the "programming in C on your Rasperry Pi" tutorials.

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. Linux Programming
  5. Raspberry Pi Programming
  6. Start Programming C with raspberry Pi

©2014 About.com. All rights reserved.