问题描述
我做了一个包含以下code进行dylib:
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
我编译dylib并把它放在另一个项目,但我似乎无法弄清楚如何从dylib进行测试对象,并调用一些方法。
任何人都知道如何做到这一点?
谢谢,
马特
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
推荐答案
仅供参考:动态库在运行时加载。如果你不需要加载code动态,静态链接
Just fyi: Dynamic libraries are loaded at runtime. If you don't need to load code dynamically, link statically.
总之:
#import "test.h"
#include <dlfcn.h>
int main(int argc, const char *argv) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
void *handle = dlopen("test.dylib", RTLD_LAZY);
id t = [[NSClassFromString(@"Test") alloc] init];
[t beep];
[t release];
dlclose(handle);
[pool drain];
}
您会想加入一些错误检查,但是这是基本的想法。如果你想使用一个NSBundle(可能是更正确的根据具体情况,请参阅)
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)
这篇关于如何访问和一个OBJ-C应用程序中使用dylib?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!