Les Smithson's C++ for LegOS
Introduction
I couldn't find any C++ support for Markus L. Noga's excellent LegOS Version 0.2.3, so I did it
myself. I make it available here for others to use. The work consisted
of building a C++ egcs/gcc compiler and writing some runtime support
code for it. Once this was done, I started to write some class
wrappers around the LegOS library.
Some Caveats
I've only tried this on Linux, but there's no reason it shouldn't work
on Windows.
The C++ run-time support is incomplete & not properly tested. C++
exceptions are not supported, and I haven't explored the more esoteric
features of inheritance & polymorphism. I'd appreciate some help
with this.
Downloads
- This is a binary of the gcc C & C++
H8300 cross compilers, built for RedHat 6.0 Linux. You might have to
right-click and save-as with your browser to save
it. After downloading, gunzip & untar it somewhere under
/usr/local. WARNING: This file is over 7 Megs. If you already
have the gcc source, you might prefer to build your C++ compiler from
scratch, as described below.
- This is a tar file with the C++ run-time
support code needed to compile and link C++ programs to run under
LegOS. See below for installation instructions.
- Here is a
tar file with C++ wrapper code for some of the LegOS functions.
- hello.tar is a simple C++ 'hello world'
program and its Makefile.
Building the C++ Compiler
I pretty much followed Luis Villa's LegOS
HOWTO instructions to the letter, with the following exceptions:
- I didn't use the -enable-target-optspace option compiler
configuration options.
- I built the C & C++ compilers in two steps, not one. i.e., I
ran make cross LANGUAGES=c then make cross
LANGUAGES=c++.
You might like to try the original instructions first, then try
these. It worked for me.
Runtime Support Code
LegOS 0.2.3 comes with some C++ support code in lib/c++. This
consists of a Makefile, and the files hack.c and
sensors.cpp. The Makefile builds lib/libc++, which
you should link with your C++ code. Hack.c contains some
elementary run-time functions required to link C++
applications. Sensor.cpp looks like wrapper code for Lego
rotation sensors. I ignored it.
To get me compiling real, usable C++ LegOS programs, I hacked
hack.c some more, and added tinfo.cc to support
typeinfo stuff for virtuals. Here is
a tar file with the files required to build the new libc++.a.
Before building anything, you should modify the TOOLPREFIX line
in Makefile.common to point to the path you installed your
new compiler in.
Caveat
I don't really know what I'm doing with this stuff. I just hacked
'till things worked. In particular, I'm somewhat mystified by
tinfo.cc, which I found in the gcc source directory. Having
said that, it seems to work with simple class definitions, single
inheritance, and the virtual keyword does what it should.
Compiling C++ programs for LegOS
To compile a C++ program for LegOS, you should first build the
C++ cross-compiler as described here, then build
the modified run-time support code. This assumes
you've modified TOOLPREFIX as described above.
- Recompile your LegOS kernel with the new compiler you've just
installed. I'm not sure this is absolutely necessary, but it does no
harm.
- Add the new libc++.a library to Makefile.user by
appending a -lc++ to the LIBS macro.
- Make sure you bracket LegOS includes with extern
"C" {}.
You should now be ready to compile a C++ program & download it to
the RCX. Here is a tar file with a simple
'hello world' C++ program & makefile for you to test. The makefile
assumes its running in a directory below the main LegOS one, parallel
to the demo directory.
LegOS Wrapper Classes
I started on some rudimentary C++ wrappers around the LegOS
calls. You can download this from here. The aim was to develop classes that
are building blocks representing real Lego objects, such as motors,
sensors, things that move etc. These can then be assembled to control
real robots.
This is only the first cut. The classes are very incomplete, but
useful at demonstrating the idea. I've used them to build rovers &
line-followers.
Bloat
You will notice that the .lx files get quite large. A
simple rover written in C is about 500 bytes (including
libc.a). Written in C++, this grows to about 6k! 2.5k of this is in
wrappers++.o, and another 2.5k is in libc++.a.
Some work will have to go into making this smaller, maybe with
judicious use of inlining.
TODO List
All help appreciated!
- Get exceptions working.
- Properly test the C++ run-time support code.
- Finish the LegOS wrapper classes.
- Find the object bloat & shrink it.
A Debugger
See here for information about my attempt to
get gdb woring with legOS, C & C++.
Disclaimer
You are free to use this code in any way you see fit, subject to the
LegOS disclaimers & copyrights. I make no representations about
the suitability of this software for any purpose. It is provided
"AS-IS" without warranty of any kind, either express or implied. So
there.
Contact Me
Question, Comments, Suggestions & Bugs to
lsmithso@hare.demon.co.uk. Or see my Home Page.
(c) Copyright L. Smithson 2000. Last updated January 28
2000
$Id: lego++.html,v 1.6 2005/12/06 12:48:05 lsmithso Exp $