Sunday, January 22, 2012

Building a program which uses existing libraries

For this, we're going to build nsnake, which requires the ncurses-devel set of precompiled libraries.

Note: While you're reading this tutorial, you'll see me exit the terminal a lot... I do that so that when I start the next section, it's always with a fresh terminal window, so everyone is in the same directory. You don't HAVE to do that, but that's why I do it in these tutorials.

Download the nSnake Source
Go here, to download a copy of nSnake (as of this writing version 1.5 is the latest):
http://sourceforge.net/projects/nsnake/files/

Save the file to your Downloads directory. You can get there by clicking on Places -> Downloads

Add caption
Move the source to your build directory:
Open your build directory Places -> Home Folder
Then double-click your build directory

Open your Downloads directory.
Double-click nsnake-1.5.tar.gz to see its contents
Drag / drop the nsnake-1.5 directory into your build directory


Changing permissions to allow the installation of precompiled ARM libraries:
These steps are required due to a bug in the current virtual machine (0.2). The VM creator is aware, so these will likely not be required in future versions of the development VM:
Double-click the Raspberry Pi Development icon

Go to the ARM emulator root file system
cd ../raspberry_pi_development/f13arm-rootfs

Open the permissions on the folders WAY up. It will ask you for a password
sudo chmod -R 777 *
[sudo] password for raspberry:

The administrator password is:
password

Close that terminal window by entering:
exit


Compile for x86 / Linux
Double-click the Raspberry Pi Development icon
In the terminal window, go to the nsnake-1.5 directory
cd nsnake-1.5

Then, make the application by entering:
make

You can then run the program by entering the following command:
./bin/nsnake


Have fun, and play a round or two, you've earned it! Press Q at the main menu to quit.

Once you're done playing around, you can close the terminal by entering:
exit

Download the precompiled libraries for ARM
Ok, so the Linux VM that you're working with has all of the precompiled libraries needed already installed on it. However, the ARM emulator does not, which means nSnake will NOT compile correctly in SB2. We need to download a precompiled library before we go anywhere.

Double-click the Raspberry Pi Development icon
Enter the following to have Scratchbox make the ARM version of the precompiled ncurses-devel library available:

sb2 -eR yum install ncurses-devel

You'll get a confirmation prompt or two. The answer is:
y


Leave the current terminal by entering:
exit

Compile for ARM

Double-click the Raspberry Pi Development icon

Change to the nSnake directory
cd nsnake-1.5

Re-make the application for ARM. In this case, I'm going to use the -B switch (case sensitive!) which will cause the whole application to re-compile from scratch. Why? Well, the current intermediate files (which are still valid / up-to-date) are in the wrong file format (x86 instead of ARM). If you try just a straight make, it will fail. -B will insure all intermediate files are compiled into ARM binaries.

To make nSnake for ARM, and forcing all fresh intermediate files, enter:
sb2 make -B

One more step before you run the ARM version: You need to set an environment variable for compatibility/emulation reasons. In your terminal window, enter:

TERM=linux

Then, as you'd expect at this point:
sb2 ./bin/nsnake

Enjoy, your game!

Hello World

In the last post, I talked about how to get a development environment up and running on your PC. You'll need to do that if you haven't done so already: http://dracosoftware.blogspot.com/2012/01/developing-in-virtual-machine.html

2012-01-22 Since the initial writing, there has been a new virtual machine released, which streamlines some of this. This post has been updated to reflect these changes.
Let's get some actual code compiled!

Login like you did in the first tutorial, and you should see the Linux desktop:



 
Double click the Raspberry Pi Development icon to start a terminal session.

My version of the virtual machine came with a "Hello World" example already in place. That's great for checking validity of the system, but not so hot for learning how to do things from the ground up... So let's create hello2.

Make a directory for our Hello, World program
mkdir hello2

Go into it
cd hello2

Now we need to enter a text editor. Linux has a variety of text editors you can use (with lots of heated debate as to which is best). For now I'm going to use Pico because it's quick/easy for folks more familiar with Windows.

pico hello2.cpp


Now, you can copy/paste the following source into pico by copying here, and then right-clicking in the terminal window and selecting Paste

/* Hello World program */
#include <stdio.h>
main()
{
    printf("Hello, world!\n");
}

Press Ctrl-X to exit Pico

It will ask you if you want to save the buffer. Press Y
Confirm the file name: hello2.cpp


Now we're back to the command prompt. We're going to compile the code you've just written using a tool called Scratchbox2. Scratchbox2's main job is to provide a cross-compiling environment, in this case for ARM. It also provides an emulator so that you can run the binaries we'll be creating... Seriously handy stuff.

You can go into ScratchBox2 in either single-command or interactive mode. I prefer single-command mode so I don't have to drop in/out of interactive mode as I edit files in pico (if you're using vi or another editor, you may not have this problem).

So, to run a command under ScratchBox2, you type:
sb2 [command]

So, for us to compile our source file to an executable file, called "hello" (note the lack of extension, this is on purpose for Linux), we issue the following command:
sb2 gcc hello2.cpp -o hello2


If you want to learn about a file (such as check to see what environment it's compiled for), you use the file command. In this case, we don't need to emulate that in ScratchBox, so we enter:
file hello2

The response should be a bit like this (target cpu emphasis mine)
hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.16, not stripped

To run our program in ScratchBox's emulator, enter the following:
sb2 ./hello2


Congrats, you now have Hello, World running in an emulated ARM environment! You can exit the terminal by entering:
exit

Monday, January 16, 2012

Developing in a Virtual Machine

Ok, so right now there is an extreme shortage of actual Raspberry Pi computers. Thankfully, to help me get started on my journey regardless, there is a virtual machine which can be used to start developing for Raspberry Pi (This is different and distinct from a Raspberry Pi virtual machine, which does not exist to my knowledge).

2012-01-22 Since the initial writing, there has been a new virtual machine released, which streamlines some of this. This post has been updated to reflect these changes.

First, you'll need to download and install VirtualBox, a free VM environment (as of this writing version 4.1.8 is the latest).
https://www.virtualbox.org/wiki/Downloads


After you've installed VirtualBox, you will want to download the VirtualBox extensions.
http://www.oracle.com/technetwork/server-storage/virtualbox/downloads/index.html#extpack


Then, you'll need a copy of the Raspberry Pi virtual machine. The developer who has created the VM has put a torrent to the VM here (it's 1.9 gigabytes, so local hosting is... tricky).
http://russelldavis.org/2012/01/20/new-raspberry-pi-development-vm-v0-2/

Don't have a torrent client? This one seems to be a good mix of usability and low-invasiveness:
http://www.bittorrent.com/downloads/complete?os=win

Once you have VirtualBox installed, open the zip and put the VM image (RaspberryPi-Development-v0.2.ova) somewhere convenient. You should be able to just double-click the ova file to get the VM imported.

Start the VM, click the Raspberry Pi Developer account, and login with a password of:  
password


 Poof, before you know it: A Raspberry Pi dev environment running in a VM.

 Next up, compiling and running "Hello world".

Getting Started with Raspberry Pi

Ok, like many people, I'm thrilled about Raspberry Pi. A small/cheap computer, that still has enough functionality to actually DO stuff.

They're $25, or $35 depending on configuration. You can learn more about them at http://www.raspberrypi.org. The only real "downside" is that they run Linux (not surprising since you could buy four of them for the same price as a copy of Windows).


Like many geeks, I have a small computer at home that I use to store my files, perform backups, run FTP, subversion, check if my websites are up, etc. This computer cost me $500, and is in the process of dying. I don't need something powerful, I need something that works and that I can leave on 24/7, while using as little power as possible.

I'm pretty sure a Raspberry Pi will be able to meet all of my computational needs for this kind of low-tech home server.

I'm a programmer, but I'm a Windows guy, not a Linux guy. That means there's going to be a considerable learning curve as I work toward making this little box do the kinds of monitoring and maintenance that I'll be looking to have it do. For the immediate future, I'm hoping to document my progress here as a series of tutorials, to make it easy and straightforward for folks to do the kinds of things that I'm looking to do.