六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 31|回复: 0

如何通过makefile在iphone下创建dylib

[复制链接]

升级  32%

86

主题

86

主题

86

主题

举人

Rank: 3Rank: 3

积分
296
 楼主| 发表于 2013-1-26 13:43:28 | 显示全部楼层 |阅读模式
首先我要声明下经过测试,第三方的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);
 


你可以去测试下了,不过相信结果应该不会很让你满意。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表