CPP Programming

From wikiPodLinux

The article's title is incorrect due to technical limitations. The correct title is: C++ Programming.

C++ works very well on the iPod. There are, however, a few pitfalls. All the pointers in iPod Programming apply here as well.

Table of contents

Hello World in C++

This can be done just as in C. The STL will work fine, but see below.

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    return 0;
}

Let's compile this with the ARM toolchain.

% arm-elf-g++ -Wl,-elf2flt -o hello hello.cc

Now `hello' can be copied to the iPod and run.

Global Objects

One hiccup you might run into concerns global constructors and destructors. As you know, in C++ constructors must be called before an object can be used; thus, global objects need their constructors called before main(). Sadly, the ARM toolchain contains a bug in its implementation of this fact. Errors about undefined reference to __CTOR_LIST__ and friends indicate that you've encountered the bug. It is very likely that you will see it in a configure script. The fix, thankfully, is simple:

Create a file ccfix.c with the following contents: int __CTOR_LIST__, __DTOR_LIST__; Compile the file: arm-elf-gcc -c -o ccfix.o ccfix.c Put it in libgcc: arm-elf-ar r /usr/local/lib/gcc-lib/arm-elf/2.95.3/libgcc.a ccfix.o At the beginning of the C++ file containing main(), insert extern "C" void __do_global_ctors(); At the beginning of main(), insert __do_global_ctors(); Your global objects should now construct successfully. (If you want destruction, repeat the last two steps with __do_global_dtors() and at the end of main().)

STL

The STL (template library) should work fine on the iPod; however, you will get a harmless message at link time.

/usr/local/arm-elf/lib/libstdc++.a(locale_impl.o): In function `_STL::locale::global(_STL::locale const &)':
locale_impl.o(.text+0x2bbc): the 'setlocale' function supports only C|POSIX locales

There is no known way to avoid this message, but it is safe to ignore it.


PZ2 Modules

You can write modules in C++ for podzilla2, but you must be careful with regards to name mangling. In particular, the PZ_MOD_INIT macro tries to create a specially named function __init_module__ to be called when the module is loaded. By default, a C++ compiler will mangle this symbol to include type information. To get your module to initialize properly, you must use extern "C":

extern "C" {
PZ_MOD_INIT(MyClass::InitializeStatic);
}

Also, make sure to include a line in your Module file indicating a dependency on libstdcxx:

Dependencies: libstdcxx