通過Makefile在iPhone下創(chuàng)建Dylib實例
通過Makefile在iPhone下創(chuàng)建Dylib實例是本文要介紹的內(nèi)容,首先我要聲明下經(jīng)過測試,第三方的dylib是無法在未越獄的iphone上c成功運行的。寫這篇文章也只是為了完善之前的那篇文章。
1、創(chuàng)建dylibtest.c 和.h
這里隨便寫了個test函數(shù)。
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
關(guān)鍵代碼如下:
- 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);
你可以去測試下了,不過相信結(jié)果應該不會很讓你滿意。
小結(jié):通過Makefile在iPhone下創(chuàng)建Dylib實例的內(nèi)容介紹完了,希望本文對你有所幫助!