Box2D Forums

It is currently Fri May 24, 2013 11:53 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 14 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Sat Aug 16, 2008 4:58 am 
Offline

Joined: Sat Aug 16, 2008 2:15 am
Posts: 10
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!


Top
 Profile  
 
PostPosted: Sat Aug 16, 2008 11:58 am 
Offline
Site Admin

Joined: Thu Sep 06, 2007 12:34 am
Posts: 2931
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.


Top
 Profile  
 
PostPosted: Sat Aug 16, 2008 12:24 pm 
Offline

Joined: Sat Aug 16, 2008 2:15 am
Posts: 10
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??? :(


Top
 Profile  
 
PostPosted: Sat Aug 16, 2008 8:23 pm 
Offline

Joined: Sun Sep 23, 2007 2:35 pm
Posts: 803
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...


Top
 Profile  
 
PostPosted: Thu Aug 21, 2008 9:40 am 
Offline

Joined: Sat Aug 09, 2008 9:56 am
Posts: 5
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).


Last edited by sonny_jim on Fri Jan 02, 2009 9:00 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Fri Aug 22, 2008 3:28 am 
Offline

Joined: Sat Aug 16, 2008 2:15 am
Posts: 10
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. :(


Top
 Profile  
 
PostPosted: Tue Oct 28, 2008 9:22 pm 
Offline

Joined: Tue Oct 28, 2008 9:17 pm
Posts: 2
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?


Top
 Profile  
 
PostPosted: Wed Oct 29, 2008 2:02 am 
Offline

Joined: Wed Sep 10, 2008 8:03 am
Posts: 138
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);


Top
 Profile  
 
PostPosted: Wed Oct 29, 2008 10:29 pm 
Offline

Joined: Tue Oct 28, 2008 9:17 pm
Posts: 2
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!!


Top
 Profile  
 
PostPosted: Mon Jan 19, 2009 7:34 am 
Offline

Joined: Mon Jan 19, 2009 7:27 am
Posts: 1
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ]  Go to page 1, 2  Next

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group