How to add Box2d to an existing Xcode/Cocos2d Project
Asked Answered
C

3

5

I have a working Xcode project that includes Cocos2d in one of the views. I now need to add Box2d. I tried dragging the Box2D folder into the libs directory of my cocos2d-2.x-ARC-iOS folder and adding to that target, but I'm getting this when I compile:

(in b2BroadPhase.cpp)
'Box2D/Collision/b2BroadPhase.h' file not found

I'm sure this is just the first of many errors I will get. What step am I missing to get all the Box2d files compiled in my project?

All of the box2d files are listed in the Compile Sources for the cocos2d-library target.

And why is this so difficult to do? Why doesn't it work like any other library?

AND - do I have to rename every single .m in my project to .mm? That's pretty much impossible as this is a large xcode project with many files. I'm just trying to use Box2d in one view.


Additional info in response to the answer given by LearnCocos2D-

My folder structure (in Finder) looks like this:

Root = MyProject.xcodeproj, MyProject folder, cocos2d-2.x-ARC-iOS folder In the cocos2d-2.x-ARC-iOS folder there is a folder called libs. Inside that folder I have the Box2D folder, along with Cocos2d, CocosDenshion, etc.

In XCode I have a PROJECT (MyProject) And 2 TARGETS (cocos2d-library, MyProject) There are "Header Search Paths" for both the PROJECT, and the TARGETS. Which one do I set? And should it be recursive or non? And when I drag the Box2D folder into Xcode, which target do i set it to?

My project file is located at /Users/me/Dropbox/iOS/MyProject. So what exactly do I enter for the Header Search Path? /Users/me/Dropbox/iOS/MyProject/cocos2d-2.x-ARC-iOS/libs doesn't seem to work.


Another Update - /Users/me/Dropbox/iOS/MyProject/cocos2d-2.x-ARC-iOS/libs as the Header Search Path in the MyProject TARGET seems to get me past the Box2D errors. Then I had a 'cassert' file not found error. Got past that by changing the view controller, it's parent, and the AppDelegate to a File Type of "Objective-C++ Source" like you suggested. And now...

Now I've got a litany of errors in NSObjCRuntime.h NSZone.h, NSObject (Expected unqualified-id, Unknown type name 'NSString', etc.). So it seems like you either create an app with Box2d/Cocos2d, or a UIKit app. Trying to have both is a complete nightmare.

Calvinism answered 2/3, 2013 at 20:43 Comment(0)
C
8

Easiest solution: start a new project with Box2D already integrated. Then add your source files & resources to it. Trust me, it doesn't get easier.

A couple things from the top of my head that are required for Box2D:

As search path use the path to the Box2D.h file minus one level. Ok, this needs a better explanation:

If Box2D.h is in /Projects/MyProject/libs/Box2D

… then search path should be /Projects/MyProject/libs

This is because Box2D #include statements always include the Box2D path:

#include <Box2D/Common/b2Settings.h>

In summary, the Header Search Path plus the #include/#import path should give the full path to the file:

/Projects/MyProject/libs/Box2D/Common/b2Settings.h

Then it's easy to check whether you got the search path right or not.

Be sure to add the search path to Header Search Path and not User Header Search Path setting as for other libraries. This is because Box2D uses angle bracket <file.h> includes. Most other libraries use "file.h" instead, which use the User Header Search Path to find their header files.

Why is it difficult? Compiler settings. There are plenty of them. Errors and warnings aren't always obvious. Actually, this is the norm. And plenty of ways to write and add code. Oh so many different languages and language versions, too. It's mostly a matter of experience. At level 5 and higher it gets easier, at level 10 it's merely a nuisance. :)

You have to rename every .m file that includes Box2D either directly (#import "Box2D.h") or indirectly (including a header that includes Box2D). If you add the Box2D header to the Prefix.pch file, that would be the same as including it in every file - so I wouldn't do that in your situation.

You can avoid the rename by setting the file type of the .m files in Xcode to "Objective-C++". Though this will be just as tedious, if not more. Tip: there are good tools out there for mass-renaming files, for situations exactly like this.

Coven answered 2/3, 2013 at 23:43 Comment(4)
Thanks so much for trying to help. I'm going to explain my current attempt at this in an edit to my original question, because I'm still getting the same error.Calvinism
I have made the edits above. Can you also elaborate on how to check if I got the search path right? I didn't understand that part of your response. Thanks!Calvinism
Minus one level because Box2D already include the Box2D folder in the search path is what made it finally work for me! AND dont forget to put the serach path between "" like if Box2D is in your project folder "$(SRCROOT)" will do the trickProportional
There must be an easier way to add an existing library.Nutt
D
2

Add this to the header search paths and see the magic work: $[PROJECT_DIR]

Duramen answered 9/6, 2014 at 20:27 Comment(0)
I
1

You may have the following compilation errors:

  1. “Header files not found”: Solution is modify the User Header Search Path to include Box2D’s directory. If you put Box2D underneath your project’s directory you may use the following environment variable ${PROJECT_DIR}. Remember to check “recursive”. NOTE: Remove all link to Box2D from Header Path. For example: TestGame/Libraries/Box2D -> ${PROJECT_DIR}/TestGame/Libraries

  2. Cpp header files not found such as <cassert> ...: rename your file to the extension *.mm instead of *.m will solve this problem.

Indemonstrable answered 27/6, 2015 at 3:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.