Reputation: 21
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
Upvotes: 2
Views: 1900
Reputation: 498
Just fyi: Dynamic libraries are loaded at runtime. If you don't need to load code dynamically, link statically.
Anyway:
#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];
}
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)
Upvotes: 2