Can't Compile on iPhone SDK and Xcode (no toolchain please)

I spent hours researching with Google and trying different ways of getting the Box2D source files to integrate with my codebase (which isn't much code, just a hello world). I know the Xcode's compiler can take C++, but when using the iPhone SDK's project that is not the case. I made sure not to change the physical folders and files, but moving the root "Include" and "Source" folders to my iPhone SDK's project and then referencing it by dragging and dropping to Xcode. When I added one line #import "Box2D.h" in my objective c file and then press "build", I get these errors below:

Code:
Line Location b2Math.h:23: error: cmath: No such file or directory
Line Location b2Math.h:24: error: cfloat: No such file or directory
Line Location b2Math.h:25: error: cstdlib: No such file or directory
Line Location b2Math.h:63: warning: implicit declaration of function 'finite'
Line Location b2Math.h:99: warning: no semicolon at end of struct or union
Line Location b2Math.h:99: error: syntax error before 'b2Vec2'
Line Location b2Math.h:102: error: syntax error before ':' token
Line Location b2Math.h:105: error: 'y' undeclared (first use in this function)
Line Location b2Math.h:105: error: 'x' undeclared (first use in this function)
Line Location b2Math.h:108: error: 'y' undeclared (first use in this function)
Line Location b2Math.h:108: error: 'x' undeclared (first use in this function)
Line Location b2Math.h:111: fatal error: method definition not in @implementation context
Line Location b2Math.h:111: error: syntax error before 'operator'
Line Location b2Settings.h:77: error: initializer element is not constant
Line Location b2Settings.h:87: error: initializer element is not constant
Line Location b2Settings.h:92: error: initializer element is not constant
Line Location b2Settings.h:107: error: initializer element is not constant
Line Location b2Settings.h:115: error: initializer element is not constant
Line Location b2Settings.h:122: error: initializer element is not constant
Line Location b2Settings.h:162: warning: type defaults to 'int' in declaration of 'b2_version'
Line Location b2Settings.h:162: error: syntax error before 'b2_version'
Line Location b2Settings.h:162: warning: data definition has no type or storage class

I also try changing the path for "No such file or directory" like this:
Code:
#import "c++/4.0.0/cmath.h"
But, it didn't work, I got more errors coming from the "cmath.h" itself.

I look into using "dylib" compiled from the Box2D project, but it is whole another problem.

About my macbook's:
Mac OS X 10.5.4 , Xcode 3.1, iPhone SDK 2.0

Please help! Thanks!
I don't know anything about iPhone development, but are you sure your project can handle C++? Try including some simple C++ class rather than Box2D.
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!

Thanks anyway for your help, sir. 🙂

Now, the next problem is I can't put C++ code with Objective C in ".h" files, because I need it to declare C++ objects so I can use it within every method??? 🙁
I'm just getting into iPhone development myself, so I know very little about it, but even if you're just interested in looking at the source rather than using it, you might want to check out the Oolong Engine (http://www.oolongengine.com/), which has a healthy dose of C++/Objective C mixing - it's pretty clear that the developers are not Objective C programmers, but that's actually kind of nice because the engine appears geared to those that would rather program C++ anyways. You might at least see what they have done to wrap the Objective C stuff so that it's usable without writing "real" Objective C...

Also, the Oolong engine has a couple of Bullet demos, which will give you some idea how to integrate a straight C++ physics engine. It seems very straightforward, all the app logic is free of Objective C.

Quote:
Now, the next problem is I can't put C++ code with Objective C in ".h" files, because I need it to declare C++ objects so I can use it within every method???

Hm, not sure about that one. You might want to hit IRC (I believe the #iphonedev channel on freenode is the place to go) and ask some of the experts...
diehard wrote:
...
Now, the next problem is I can't put C++ code with Objective C in ".h" files, because I need it to declare C++ objects so I can use it within every method??? 🙁

Well, from my experience, when you use the C++ Box2D files in an Objective-C header file, it works just fine. Here's a simplified example; I followed your advice of putting the /source and /include directories into the project.

Code:
// == HEADER : something.h == //

#import <UIKit/UIKit.h>
#import "Box2D.h"


@interface something : NSObject
{
    b2World* myWorld;
    // et cetera ... //
}

@property (nonatomic) b2World* world;
// other properties ... //

- (b2World*)doSomething:(b2Body*)b withWorld:(b2World*)w;
// other methods ... //

@end


//-------------------------------------//


// == IMPLEMENTATION : something.mm == //

#import "something.h"


@implementation something

@synthesize world = myWorld;

// init, dealloc, and all that other good stuff... //

- (b2World*)doSomething:(b2Body*)b withWorld:(b2World*)w
{
    b2World* aWorld;
    // do something... //
    return aWorld;
}

@end

(btw, thanks for the mm trick; I never knew that you could enable mixed content with the .mm extension!)

(edit 01.i.08 : non-NSObject properties can't have the "retain" attribute).
I try including the #import "Box2D.h" in my .h objective-c header file, but I get errors before I add the C++ objects in the @interface. I don't know how you successfully compile without errors and warnings? Am I missing something?

I find that if you include @interface in the .mm file it works, but the @interface name has to be different from the @interface in the .h objective-c header file.

Learning by trial and error. 🙁
I've been unable to #import the Box2D.h header file in an Objective-C header file myself.

However I'm running into a bunch of other problems. For starters, I keep getting segfaults every time I try to pass an object into a method as an argument.

Code:
b2AABB worldAABB;
worldAABB.lowerBound.Set(100.0f, -100.0f);
worldAABB.lowerBound.Set(100.0f, 100.0f);
b2Vec2 gravity(0.0f, -10.0f);
b2World world(worldAABB, gravity, true); // SIGABRT here


I'm probably doing something silly. Anyone care to point out my silliness?
You set the lower bound twice, an never set the upper bound?

Suggesting:
Code:
worldAABB.lowerBound.Set(-100.0f, -100.0f);
worldAABB.upperBound.Set(100.0f, 100.0f);
Oh jee, I can't believe that was it. I guess I shouldn't be doing twenty different things at once while doing Box2D and Objective-C.

Thanks!!
diehard,

I'm running into similar compilation problems. Were you ever successful? If so, would it be possible to get your xproj and directory structure? Thanks.

Original thread: https://box2d.org/forum/viewtopic.php?t=1283