如何在 Obj-C 应用程序中访问和使用 dylib?
我制作了一个包含以下代码的 dylib:
Test.h:
#import <Cocoa/Cocoa.h>
@interface Test : NSObject {
int number;
}
-(void)beep;
-(void)giveInt:(int)num;
-(int)takeInt;
@end
Test.m:
#import "Test.h"
@implementation Test
-(void)beep {
NSBeep();
}
-(void)giveInt:(int)num {
number = num;
}
-(int)takeInt {
return number;
}
@end
我已经编译了 dylib 并将其放入另一个项目中,但我似乎无法弄清楚如何从 dylib 中创建一个 Test 对象并调用一些方法。< br> 有人知道该怎么做吗?
谢谢,
马特
I've made a dylib that contains the following code:
Test.h:
#import <Cocoa/Cocoa.h>
@interface Test : NSObject {
int number;
}
-(void)beep;
-(void)giveInt:(int)num;
-(int)takeInt;
@end
Test.m:
#import "Test.h"
@implementation Test
-(void)beep {
NSBeep();
}
-(void)giveInt:(int)num {
number = num;
}
-(int)takeInt {
return number;
}
@end
I've compiled the dylib and put it in another project but I can't seem to figure out how to make a Test object from the dylib and call some of the methods.
Anyone know how to do this?
Thanks,
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅供参考:动态库是在运行时加载的。如果不需要动态加载代码,请静态链接。
无论如何:
您需要包含一些错误检查,但这是基本思想。如果您想使用 NSBundle (根据具体情况,这可能更“合适”,请参阅 http://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html)
Just fyi: Dynamic libraries are loaded at runtime. If you don't need to load code dynamically, link statically.
Anyway:
You'll want to include some error checking, but that's the basic idea. If you want to use NSBundle (which may be more "proper" depending on the circumstances, see http://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html)