如何将 Chipmunk 库包含到 iPhone Xcode 项目中?
[已解决]
我从 cocos2d+chipmunk 模板复制了chipmunk文件夹结构并构建成功。
- Classes/Chipmunk/include/src 用于“src”文件夹
- Classes/Chipmunk/chipmunk 用于“include”文件夹
感谢 Beta 的帮助。
:::::
我下载了chipmunk 5.3.1 并尝试使用一个简单的示例,但收到此编译错误:
Undefined symbols:
"_cpSpaceStep", referenced from:
-[ChipmunkTestViewController delta:] in ChipmunkTestViewController.o
"_cpBodyNew", referenced from:
-[ChipmunkTestViewController configurarChipmunk] in ChipmunkTestViewController.o
"_cpSpaceAddShape", referenced from:
-[ChipmunkTestViewController configurarChipmunk] in ChipmunkTestViewController.o
"_cpSpaceAddBody", referenced from:
-[ChipmunkTestViewController configurarChipmunk] in ChipmunkTestViewController.o
"_cpSpaceHashEach", referenced from:
-[ChipmunkTestViewController delta:] in ChipmunkTestViewController.o
"_cpInitChipmunk", referenced from:
-[ChipmunkTestViewController configurarChipmunk] in ChipmunkTestViewController.o
"_cpCircleShapeNew", referenced from:
-[ChipmunkTestViewController configurarChipmunk] in ChipmunkTestViewController.o
"_cpSpaceNew", referenced from:
-[ChipmunkTestViewController configurarChipmunk] in ChipmunkTestViewController.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
我不确定是否正确添加了Chipmunk 库,我必须包含chipmunk .tgz 中的哪些源?
代码如下:
ChipmunkTestViewController.h
#import <UIKit/UIKit.h>
#import "chipmunk.h"
@interface ChipmunkTestViewController : UIViewController {
UIImageView *barra;
UIImageView *esfera;
cpSpace *space;
}
- (void) configurarChipmunk;
- (void) delta:(NSTimer *)timer;
void updateShape(void *ptr, void *unused);
@end
ChipmunkTestViewController.m
#import "ChipmunkTestViewController.h"
@implementation ChipmunkTestViewController
- (void) configurarChipmunk {
cpInitChipmunk(); // Init Chipmunk engine
space = cpSpaceNew(); // Create new Space
space->gravity = cpv(0, -100); // Direcction and magnitude of gravity in Space
[NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(delta:) userInfo:nil repeats:YES]; // NSTimer for animations
// Create esfera Body
cpBody *esferaBody = cpBodyNew(50.0f, INFINITY);
esferaBody->p = cpv(160,250);
// Create esfera Shape
cpShape *esferaShape = cpCircleShapeNew(esferaBody, 15.0f, cpvzero);
esferaShape->e = 0.5f; // Elasticity
esferaShape->u = 0.8f; // Friction
esferaShape->data = esfera; // UIImageView association
esferaShape->collision_type = 1;
cpSpaceAddBody(space, esferaBody);
cpSpaceAddShape(space, esferaShape);
}
- (void) delta:(NSTimer *)timer {
cpSpaceStep(space, 1.0f/60.0f); // Refresh Space info
cpSpaceHashEach(space->activeShapes, &updateShape, nil); // Refresh Shapes info
}
void updateShape(void *ptr, void *unused) {
cpShape *shape = (cpShape*)ptr;
if (shape == nil || shape->body == nil || shape->data == nil) {
NSLog(@"Invalid Shape...");
return;
}
// Refresh Shape position
if ([(UIView*)shape->data isKindOfClass:[UIView class]]) {
[(UIView*)shape->data setCenter:CGPointMake(shape->body->p.x, 480 - shape->body->p.y)];
} else {
NSLog(@"Shape updated outside updateShape function...");
}
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
barra = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"barra.png"]];
barra.center = CGPointMake(160, 350);
esfera = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"esfera.png"]];
esfera.center = CGPointMake(160, 230);
[self.view addSubview:barra];
[self.view addSubview:esfera];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self configurarChipmunk];
}
...
@end
[SOLVED]
I copy the chipmunk folder structure from cocos2d+chipmunk template and build OK.
- Classes/Chipmunk/include/src for 'src' folder
- Classes/Chipmunk/chipmunk for 'include' folder
Thanks to Beta for trying to help.
:::::
I download chipmunk 5.3.1 and try with a simple example but I receive this compiled errors:
Undefined symbols:
"_cpSpaceStep", referenced from:
-[ChipmunkTestViewController delta:] in ChipmunkTestViewController.o
"_cpBodyNew", referenced from:
-[ChipmunkTestViewController configurarChipmunk] in ChipmunkTestViewController.o
"_cpSpaceAddShape", referenced from:
-[ChipmunkTestViewController configurarChipmunk] in ChipmunkTestViewController.o
"_cpSpaceAddBody", referenced from:
-[ChipmunkTestViewController configurarChipmunk] in ChipmunkTestViewController.o
"_cpSpaceHashEach", referenced from:
-[ChipmunkTestViewController delta:] in ChipmunkTestViewController.o
"_cpInitChipmunk", referenced from:
-[ChipmunkTestViewController configurarChipmunk] in ChipmunkTestViewController.o
"_cpCircleShapeNew", referenced from:
-[ChipmunkTestViewController configurarChipmunk] in ChipmunkTestViewController.o
"_cpSpaceNew", referenced from:
-[ChipmunkTestViewController configurarChipmunk] in ChipmunkTestViewController.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
I'm not sure about adding Chipmunk libraries correctly, what sources from chipmunk .tgz I have to include?
Here's the code:
ChipmunkTestViewController.h
#import <UIKit/UIKit.h>
#import "chipmunk.h"
@interface ChipmunkTestViewController : UIViewController {
UIImageView *barra;
UIImageView *esfera;
cpSpace *space;
}
- (void) configurarChipmunk;
- (void) delta:(NSTimer *)timer;
void updateShape(void *ptr, void *unused);
@end
ChipmunkTestViewController.m
#import "ChipmunkTestViewController.h"
@implementation ChipmunkTestViewController
- (void) configurarChipmunk {
cpInitChipmunk(); // Init Chipmunk engine
space = cpSpaceNew(); // Create new Space
space->gravity = cpv(0, -100); // Direcction and magnitude of gravity in Space
[NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(delta:) userInfo:nil repeats:YES]; // NSTimer for animations
// Create esfera Body
cpBody *esferaBody = cpBodyNew(50.0f, INFINITY);
esferaBody->p = cpv(160,250);
// Create esfera Shape
cpShape *esferaShape = cpCircleShapeNew(esferaBody, 15.0f, cpvzero);
esferaShape->e = 0.5f; // Elasticity
esferaShape->u = 0.8f; // Friction
esferaShape->data = esfera; // UIImageView association
esferaShape->collision_type = 1;
cpSpaceAddBody(space, esferaBody);
cpSpaceAddShape(space, esferaShape);
}
- (void) delta:(NSTimer *)timer {
cpSpaceStep(space, 1.0f/60.0f); // Refresh Space info
cpSpaceHashEach(space->activeShapes, &updateShape, nil); // Refresh Shapes info
}
void updateShape(void *ptr, void *unused) {
cpShape *shape = (cpShape*)ptr;
if (shape == nil || shape->body == nil || shape->data == nil) {
NSLog(@"Invalid Shape...");
return;
}
// Refresh Shape position
if ([(UIView*)shape->data isKindOfClass:[UIView class]]) {
[(UIView*)shape->data setCenter:CGPointMake(shape->body->p.x, 480 - shape->body->p.y)];
} else {
NSLog(@"Shape updated outside updateShape function...");
}
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
barra = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"barra.png"]];
barra.center = CGPointMake(160, 350);
esfera = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"esfera.png"]];
esfera.center = CGPointMake(160, 230);
[self.view addSubview:barra];
[self.view addSubview:esfera];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self configurarChipmunk];
}
...
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用 macosx/ 目录中的 iphonestatic.command 脚本来构建静态库并复制标头,如自述文件所述。然后您所要做的就是将该文件夹放入您的项目中。
如果您只是将源代码复制到项目中,那么您几乎肯定会丢失几个非常重要的优化标志。不要这样做!
You should use the iphonestatic.command script in the macosx/ directory to build a static library and copy the headers for you like the README says. Then all you have to do is drop that folder into your project.
If you are just copying the sources into your project you are almost certainly missing several very important optimization flags. Don't do it!
我从 cocos2d+chipmunk 模板复制了chipmunk文件夹结构并构建成功。
I copy the chipmunk folder structure from cocos2d+chipmunk template and build OK.
我今天有同样的问题,我这样做了:
1.- 转到项目 -->添加到项目并找到 cocos2d-iphone-0.99.5 文件,然后从该目录中添加 External 目录(包含花栗鼠文件)。请务必选中“将项目复制到目标组的文件夹(如果需要)”旁边的复选框,然后单击“添加”按钮。
2.- 我遵循本教程: http://monoclestudios.com/cocos2d_whitepaper.html (在中间该页面的内容是需要添加花栗鼠的所有信息)
3.- 将#include“constraints/util.h”声明更改为:“#include util.h”
我认为已经完成。
I had the same question this days and I did this:
1.- Go to project --> add to project and find cocos2d-iphone-0.99.5 file, then from within that directory I've added the External directory (containing the chipmunk files). Be sure to have a checkmark in the box next to Copy items into destination group's folder (if needed), and then click the Add button.
2.- I follow this tutorial: http://monoclestudios.com/cocos2d_whitepaper.html (in the middle of the page is all the information need to add chipmunk)
3.- Change the #include "constraints/util.h" declaration to: "#include util.h"
I think is done.
如果您使用 CocoaPods:
YES,将“${PODS_ROOT}/Headers/Private/chipmunk-physicals”设置为递归,并将
“$ {PODS_ROOT}/Headers/Public/chipmunk-physicals”到递归
希望对某人有帮助
If you use CocoaPods:
"${PODS_ROOT}/Headers/Private/chipmunk-physics" to Recursive and
"${PODS_ROOT}/Headers/Public/chipmunk-physics" to Recursive
hope it helps someone