首先我要声明下经过测试,第三方的dylib是无法在未越狱的iphone上c成功运行的。写这篇文章也只是为了完善之前的那篇文章。
1、创建dylibtest.c 和.h
没神马好说的,想写神马就写神马吧。。。这里随便写了个test函数
################ dylibtest.h ######################
void test();
############### dylibtest.c ###########################
#include "dylibtest.h"
#include "stdio.h"
void test() {
printf("this is a test\n");
}
############# makefile ###########################
CC=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc
CFLAGS= -arch armv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk
CPP=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/cpp
target:
$(CC) $(CFLAGS) -dynamiclib -o sotest-iphone.dylib dylibtest.c
这里用的是sdk4.2 arch为armv6,另外需要提醒的事,如果你编译的是.m文件使用到framework的话编译时可以这样写
-framework Foundation
下面是测试代码 testdylib
关键代码如下:
NSString *path = [[NSBundle mainBundle] pathForResource:@"sotest-iphone" ofType:@"dylib"];
void* handle = dlopen([path cStringUsingEncoding:NSUTF8StringEncoding], RTLD_LAZY);
if (!handle) {
printf("%s\n", dlerror());
return;
}
void (*test)();
test = (void (*)())dlsym(handle, "test");
const char *dlsym_error = dlerror();
if (dlsym_error) {
printf("%s\n", dlsym_error);
dlclose(handle);
return;
}
// use it to do the calculation
test();
// close the library
dlclose(handle);
你可以去测试下了,不过相信结果应该不会很让你满意。