如何通过makefile在iphone下创建dylib

首先我要声明下经过测试,第三方的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 mainBundlepathForResource:@"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);

 


你可以去测试下了,不过相信结果应该不会很让你满意。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值