iOSでdynamic library
iPhone で動く dynamic library が作れたのでメモを残しておく。
ビルド時のオプション忘れそうなんで。
適当に testlib.c を作る。
int add(int a, int b) { return a + b; }
これを arm-apple-darwin10-gcc-4.2.1 でビルドする。
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-gcc-4.2.1 -march=armv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk -shared testlib.c -L/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk/usr/lib -o testlib.dylib
iOS4.2のSDKを明示的に指定しないとうまくいかない。1つだけワーニングが出るが問題なさそうだ。
使う側は
#include <dlfcn.h> void* libtest; int (*add_call)(); int p = 0; libtest = dlopen([ここにPathを入れる], RTLD_LAZY); if(libtest != NULL){ add_call = dlsym(libtest, "add"); p = (*add_call)(3, 7); } NSLog(@"%d", p);
これでうまくいった。