如何将 Box2d 添加到现有的 Xcode/Cocos2d 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15179253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add Box2d to an existing Xcode/Cocos2d Project
提问by soleil
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:
我有一个工作 Xcode 项目,其中一个视图包含 Cocos2d。我现在需要添加 Box2d。我尝试将 Box2D 文件夹拖到我的 cocos2d-2.x-ARC-iOS 文件夹的 libs 目录中并添加到该目标,但是我在编译时得到了这个:
(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?
我确信这只是我会遇到的许多错误中的第一个。我错过了什么步骤来编译我的项目中的所有 Box2d 文件?
All of the box2d files are listed in the Compile Sources for the cocos2d-library target.
所有 box2d 文件都列在 cocos2d-library 目标的编译源中。
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.
并且 - 我是否必须将项目中的每个 .m 重命名为 .mm?这几乎是不可能的,因为这是一个包含许多文件的大型 xcode 项目。我只是想在一个视图中使用 Box2d。
Additional info in response to the answer given by LearnCocos2D-
针对 LearnCocos2D 给出的答案的附加信息-
My folder structure (in Finder) looks like this:
我的文件夹结构(在 Finder 中)如下所示:
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.
Root = MyProject.xcodeproj, MyProject 文件夹, cocos2d-2.x-ARC-iOS 文件夹在cocos2d-2.x-ARC-iOS 文件夹中有一个名为libs 的文件夹。在该文件夹中,我有 Box2D 文件夹,以及 Cocos2d、CocosDenshion 等。
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?
在 XCode 中,我有一个项目(MyProject)和 2 个目标(cocos2d-library,MyProject)项目和目标都有“标题搜索路径”。我设置哪一个?它应该是递归的还是非递归的?当我将 Box2D 文件夹拖入 Xcode 时,我将它设置为哪个目标?
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.
我的项目文件位于 /Users/me/Dropbox/iOS/MyProject。那么我到底应该为标题搜索路径输入什么?/Users/me/Dropbox/iOS/MyProject/cocos2d-2.x-ARC-iOS/libs 似乎不起作用。
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...
另一个更新 - /Users/me/Dropbox/iOS/MyProject/cocos2d-2.x-ARC-iOS/libs 作为 MyProject TARGET 中的标题搜索路径似乎让我克服了 Box2D 错误。然后我有一个'cassert'文件未找到错误。通过将视图控制器(它的父级)和 AppDelegate 更改为“Objective-C++ Source”的文件类型(如您所建议的),从而解决了这个问题。现在...
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.
现在,我在 NSObjCRuntime.h NSZone.h、NSObject(预期的非限定 ID、未知类型名称“NSString”等)中出现了一连串错误。所以看起来你要么使用 Box2d/Cocos2d 创建一个应用程序,要么创建一个 UIKit 应用程序。试图同时拥有两者是一场彻头彻尾的噩梦。
回答by LearnCocos2D
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.
最简单的解决方案:使用已经集成的 Box2D 开始一个新项目。然后将您的源文件和资源添加到其中。相信我,它并没有变得更容易。
A couple things from the top of my head that are required for Box2D:
Box2D 需要的几件事情:
As search path use the path to the Box2D.h file minus one level. Ok, this needs a better explanation:
作为搜索路径,使用 Box2D.h 文件的路径减去一级。好的,这需要更好的解释:
If Box2D.h is in /Projects/MyProject/libs/Box2D
如果 Box2D.h 在 /Projects/MyProject/libs/Box2D
… then search path should be /Projects/MyProject/libs
… 那么搜索路径应该是 /Projects/MyProject/libs
This is because Box2D #include statements always include the Box2D path:
这是因为 Box2D #include 语句总是包含 Box2D 路径:
#include <Box2D/Common/b2Settings.h>
In summary, the Header Search Path plus the #include/#import path should give the full path to the file:
总之,标题搜索路径加上#include/#import 路径应该给出文件的完整路径:
/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 Pathand 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.
确保将搜索路径添加到Header Search Path而不是 User Header Search Path 设置,就像其他库一样。这是因为 Box2D 使用尖括号<file.h>
包含。大多数其他库使用"file.h"
它,它们使用用户头搜索路径来查找它们的头文件。
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. :)
为什么很难?编译器设置。他们有很多。错误和警告并不总是显而易见的。其实,这是常态。以及多种编写和添加代码的方法。哦,还有很多不同的语言和语言版本。这主要是经验问题。在 5 级和更高级别它变得更容易,在 10 级它只是一个麻烦。:)
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.
您必须直接 ( #import "Box2D.h"
) 或间接(包括包含 Box2D 的标头)重命名包含 Box2D 的每个 .m 文件。如果您将 Box2D 标头添加到 Prefix.pch 文件中,这与将其包含在每个文件中是一样的 - 所以在您的情况下我不会这样做。
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.
您可以通过将 Xcode 中 .m 文件的文件类型设置为“Objective-C++”来避免重命名。尽管这将同样乏味,甚至更多。提示:有一些很好的工具可用于批量重命名文件,适用于这种情况。
回答by user371260
Add this to the header search paths and see the magic work: $[PROJECT_DIR]
将此添加到标题搜索路径并查看神奇的工作:$[PROJECT_DIR]
回答by Hiep Huynh Ngoc
You may have the following compilation errors:
您可能会遇到以下编译错误:
“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
Cpp header files not found such as
<cassert>
...: rename your file to the extension *.mm instead of *.m will solve this problem.
“找不到头文件”:解决方法是修改用户头搜索路径以包含 Box2D 的目录。如果将 Box2D 放在项目目录下,则可以使用以下环境变量 ${PROJECT_DIR}。记得勾选“递归”。注意:从标题路径中删除所有指向 Box2D 的链接。例如:TestGame/Libraries/Box2D -> ${PROJECT_DIR}/TestGame/Libraries
找不到 Cpp 头文件,例如
<cassert>
...:将文件重命名为扩展名 *.mm 而不是 *.m 将解决此问题。