xcode下lua扩展库的编译
来源:互联网 发布:淘宝网店加盟官网 编辑:程序博客网 时间:2024/06/05 20:13
最近在研究ios越狱下的touchsprite脚本的开发. touchsprite的脚本语言是lua, 有个需求是需要通过touchsprite批量导入联系人到ios里. 看了一下touchsprite的官方文档, 并没有找到操作通信录联系人的方法.
但是看到了可以扩展lua代码库.于是很自然的就想自己写一个lua扩展库.
首先,通过google了解了如何操作ios的通信录, 自己简单写了个ios app取名叫导入大师, 基本功能是把需要导入的电话号码都写到一个文件里,每个电话号码一行,并将文件导入手机路径/var/mobile/Media/touchsprite/lua/telphone.txt下,在app里可以设置每次,导入的号码个数.也可以清空通信录.
由于需要访问文件系统,不能简单的以ipa方式安装,需要通过cydia安装到/Applications目录下.我把他发布到我的威锋源下,有兴趣的可以在cydia里添加我的威锋源下载. 源码可以在我github上查看.
可以看到在app上是可以正常导入删除通信录的.现在就需要把导入和删除通信录的接口暴露给lua.
关于写lua扩展,之前也没有弄过, 于是又在网上一顿海搜, 终于找出了,写一个lua扩展库的基本框架.
我通过代码简要说明一下,代码是从触动精灵里开发手册扒过来的.
#include <stdio.h>#include <stdlib.h>#include "lua.h"#include "lualib.h"#include "lauxlib.h"#import <objc/runtime.h>#import <Foundation/Foundation.h>#import <UIKit/UIDevice.h>/* 库 open 函数的前置声明 */int luaopen_mt(lua_State *L);/* Function mt_get_device_name * @return string device name */static int mt_get_device_name(lua_State *L){ NSString *name = [[UIDevice currentDevice] name]; const char * name_str = [name UTF8String]; lua_pushstring(L, name_str); return 1;}//注册函数库static const luaL_Reg mt_lib[] = { {"device_name", mt_get_device_name}, //获取设备名称 {NULL, NULL}};int luaopen_mt(lua_State *L)//注意, mt为扩展库的文件名{ luaL_newlib(L, mt_lib);//暴露给lua脚本的接口 return 1;}
lua层里的调用方法为:
local mt = require "mt"print(mt.device_name())
官方文档只介绍了简单的单个文件的编译.
现在我要介绍的是如果通过xcode来编译.
默认xcode是无法为ios创建动态库工程的,
请参考http://blog.csdn.net/hursing/article/details/8951958.修改xcode模版.
配置好了之后就可以开工了,
首先,新建一个xcode动态库工程. 导入lua.h文件. 为动态库实现接口.
代码如下:
//注册函数库static const luaL_Reg mt_lib[] = { {"device_name", mt_get_device_name}, //获取设备名称 {"test", luaPlugin_test}, {"requestAddressBook", luaPlugin_requestAddressBook}, {"importConactFromFiles", luaPlugin_importConactFromFiles}, {"removeAllConacts", luaPlugin_removeAllConacts}, {"saveHistory", luaPlugin_saveHistory}, {"readHistory", luaPlugin_readHistory}, {NULL, NULL}};int luaopen_luaPlugin(lua_State *L){ luaL_newlib(L, mt_lib); return 1;}
实现各个接口之后, 编译会报错一堆这样的错误.
Undefined symbols for architecture armv7: "_lua_isnumber", referenced from: _luaPlugin_importConactFromFiles in luaPlugin.o _luaPlugin_saveHistory in luaPlugin.o "_lua_tonumberx", referenced from: _luaPlugin_importConactFromFiles in luaPlugin.o _luaPlugin_saveHistory in luaPlugin.o "_lua_pushboolean", referenced from: _luaPlugin_importConactFromFiles in luaPlugin.o "_lua_gettop", referenced from: _luaPlugin_test in luaPlugin.o _luaPlugin_importConactFromFile s in luaPlugin.o _luaPlugin_saveHistory in luaPlugin.o "_lua_error", referenced from: _luaPlugin_test in luaPlugin.o _luaPlugin_importConactFromFiles in luaPlugin.o _luaPlugin_saveHistory in luaPlugin.o "_lua_tolstring", referenced from: _luaPlugin_test in luaPlugin.o "_lua_isstring", referenced from: _luaPlugin_test in luaPlugin.o "_lua_pushstring", referenced from: _mt_get_device_name in luaPlugin.o _luaPlugin_test in luaPlugin.o _luaPlugin_importConactFromFiles in luaPlugin.o _luaPlugin_saveHistory in luaPlugin.o "_lua_createtable", referenced from: _luaopen_luaPlugin in luaPlugin.o "_lua_pushinteger", referenced from: _luaPlugin_readHistory in luaPlugin.o "_luaL_setfuncs", referenced from: _luaopen_luaPlugin in luaPlugin.old: symbol(s) not found for architecture armv7clang: error: linker command failed with exit code 1 (use -v to see invocation)
这是因为他要找函数对应的实现, 但是我们不能把lua.dylib给链接进去.我们做的是扩展库.不需要链接这些.
解决方式很简单, 只需要在xcode中 Build Setttings – Linking – other linker Falgs 中添加-undefined dynamic_lookup.重新编译即可.
如图所示:
在把luaPlugin.dylib 复制到touchsrpite的plugin目录下.
脚本里把plugin目录添加到 package.cpath里就可以直接调用
local lp = require('luaPlugin')local msg = lp.test("hello wrold");print(msg)
成功输出hello wrold.
但是奇葩的问题出现. 脚本里始终无法获得通信录里的数据,也无法添加.
我想可能是touchsprite进程所限制了.如果哪位知道怎么解决的.请告知一下.
- xcode下lua扩展库的编译
- window下编译lua源码,编译lua的库文件,编译lua解释器,编译lua编译器
- XCode编译和使用Lua的方法
- 在Xcode下编译cocos2d-x语言为lua时出错的解决方案
- Lua相关的扩展库
- Lua Lib在VC下的编译/用BCB6编译LUA库
- 在xcode上编译lua
- Lua cjson库在windows下的编译与使用
- Lua Lib在VC下的编译
- lua c在linux下的编译
- 编译linux下的lua程序失败
- Lua在VC环境下的编译
- 【Lua】Mac系统下配置SublimeText的Lua编译环境
- 最简单的Mac xcode下搭建lua
- Mac下编译PHP的pgsql扩展库方法
- ubuntu下编译lua
- Linux下Lua扩展so
- Xcode 5 环境下编译 ffmpeg 库
- PHP关联数组搜索键和值
- JS,Jquery获取各种屏幕的宽度和高度
- 【转】linux系统时间修改及同步
- SQL Server日期时间格式转换字符串
- UTF-8文件的BOM头的来由及去除方法
- xcode下lua扩展库的编译
- Excel快速输入对勾和错号
- iOS开发~CocoaPods使用详细说明
- 真是无语二手房可以这样逃税
- 数据库删除或还原提示正在使用处理…
- 学习无线传感器网络该努力的方向?
- SQLSERVER2008添加远程链接服务器
- 大型分布式网站架构技术总结
- 女人最私密的25个小秘密