Building for iPhone

From Box2D Wiki

A number of posts on the box2d forums discuss build errors when using box2d (which is written in C++) with iphone projects (which are developed with Objective-C).


.m to .mm

Apple's documentation states:

"Xcode requires that file names have a “.mm” extension for the Objective-C++ extensions to be enabled by the compiler."

A number of posts on the box2d forums seem to indicate that most if not all the problems people are having compiling box2d for iphone are resolved by changing the extension of Objective-C source files which reference box2d from '.m' to '.mm'


posted by diehard in forum: Can't Compile on iPhone SDK and Xcode (no toolchain please)

I figured it out after many hours! It was simply changing '.m' to '.mm', so Xcode's compiler can recognize the source file is mixed with Objective C and C++. I wish it was more obvious, but I guess because Objective C the programming language is not that popular (hardly getting a lot of good search results), especially when it comes to intermixing C++ with it. Yuck!


b2Math.h

The forum post Box2d on iPhone (again) mentions problems with b2IsValid in b2Math.h when compiling for the iphone device, but not when compiling for the simulator.

Tyrathect mentions:

I changed b2math.h to include math.h and float.h instead of cmath and cfloat, and I didn't have any other problems.

You can define TARGET_OS_IPHONE in Xcode, and it'll make these changes to b2math.h. Here's how to do define a preprocessor symbol in Xcode for those that don't know: http://stackoverflow.com/questions/367368/how-to-define-a-preprocessor-symbol-in-xcode

Compiler optimisations

in Box2d on iPhone (again) ewjordan mentions problems using the -O3 optimization option

'...everything was generally going nuts. It turned out to be because I had checked -O3 optimization; once I switched to -Os instead, things went back to normal...so something with the ARM optimizer is clearly messed up....if anyone else runs into trouble, try playing with the optimization settings (both per-project and per-target)."

Image:Icon-building.png‎ Building Instructions
C++ AS3
TOOLBOX
LANGUAGES