Compiling C/C++ program for the OLPC

Revision as of 14:16, 5 May 2012 by Dov (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Note: The instructions here are valid for x86 based OLPCs, i.e. the OLPC 1.0 and 1.5 models. OLPC 1.75 is ARM based, and you will need a cross compiler setup e.g. based on scratchbox. That is outside the scope of this note.


Last week I found a bug in the FBReader program and I wanted to compile a new executable for my OLPC with my bugfix. Unfortunately my development system is already updated to Fedora 8 and the executable that I get contained dependencies on shared libraries that only have older versions on the OLPC. This is not very surprising. But the question was how to create a development environment that easily lets me create executables with the correct shared library dependencies. I'm sure there are several method, but the method I used is based on chroot, and should work on any modern Linux based distribution.

Note: This assumes that you do not wish to use the XO itself to compile. Compiling on the XO is entirely possible. Normal C programs can be compiled without trouble. Projects involving C++, especially if also using Boost, may require shutting down the GUI ("telinit 3") to free up some RAM or the addition of swap space. You may install the normal development tools with a command such as "yum install gcc make cvs subversion libX11-devel".

The chroot OLPC environment

The idea is to create a complete copy of the olpc disk contents on my desktop and then chroot into this enviroment. While in the chroot environment I can do yum install to install all the development tools that I need for the compilation.

Here are the exact steps:

  • On your desktop do:
 mkdir -p /home/olpc/devp
 cd /home/olpc/devp
 mkdir dev
 mknod dev/null c 1 3
 chmod 0666 dev/null 
 mkdir proc
 mkdir src
  • On the olpc do:
 cd /
 rsync -a bin sbin lib usr etc home var desktop:/home/olpc/devp
 scp proc/cpuinfo desktop:/home/olpc/devp/proc

where desktop is the ip of your desktop. (You will also have to take care of the permissions of the copying through user@desktop or by using chmod). This copies all the os files as well as makes a virtual copy of proc/cpuinfo that yum/rpm needs.

  • Now chroot into this new enviroment on the desktop:
 chroot /home/olpc/devp
  • In the chroot jail we can now set up the development environment through yum. To compile FBReader I needed to do the following installations:
 yum -y install gcc gcc-c++ gtk2-devel make bzip2-devel \
   libz-devel libjpeg-devel

Your milage my vary, but for a gtk program the dependencies above seem resonable.

That's it more or less. You can now develop and compile software in the chroot jail and then copy it over to the OLPC and be pretty sure that the dependencies are correct.

su in the chroot jail

If you feel uncomfortable working as root in the chroot jail then you can su to the user olpc. Unfortunately this does not work as the fedora 7 su program uses PAM authentification. One way to get around this is to grab coreutils and recompile it, and copy the resulting su executable to /bin/su.


Good luck!

Please let me know if you found this info helpful, e.g. in my Discussion page.

-- User:Dov