Lua嵌入iOS/Mac工程

来源:互联网 发布:大型企业网络建设方案 编辑:程序博客网 时间:2024/06/04 19:03

Lua嵌入iOS/Mac工程

首先我们需要编译lua环境,得到 lua工程的静态库

1.到http://www.lua.org/ftp/ 下载lua源码,版本任意
2.解压后 的res文件夹便是我们的需要的lua源码
3.新建一个静态库工程 ,打开xcode —> File —>New —>Project –>选择 Static Libary,并且工程名命名为lua
这里写图片描述
4.将2步骤中的res文件放到工程,Product->Build便可以编出我们需要的 .a 包
这里写图片描述

5.新建一个iOS 工程
比如新建一个名字为 LuaOniOS 的工程。
这里写图片描述
6. 将iOS工程引用lua静态库工程。
因静态库工程是iOS工程的子工程,为管理方便,将其放到到LuaOniOS工程文件夹下
这里写图片描述
- 步骤1 将 静态库工程的 lua.xcodeproj 文件拖拽到 iOS 工程中
- 步骤2 设置LuaOniOS 的Build Settings –> Header Search Paths 添加一条 $(SRCROOT)/LuaOniOS/Lua/Lua 目的是搜索lua头文件时能够索引得到
- 步骤2 设置LuaOniOS 的Build Phases –>Link Binary With Libraries 添加 libLua.a 目的是包含子工程编译出来的静态库

7.在工程中新加一个 test.lua

print("hello lua on ios")

然后在ViewController.m中编写

#import "ViewController.h"#import "lua.h"#include "lauxlib.h"#include "lualib.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    lua_State * L = luaL_newstate();    luaL_openlibs(L);    NSString * scriptPath = [[NSBundle mainBundle]pathForResource:@"test" ofType:@"lua"];    int error;    error = luaL_dofile(L,[scriptPath cStringUsingEncoding:NSUTF8StringEncoding]); /* runs Lua script */    if (error) {        printf("%s",lua_tostring(L, -1));    }    lua_close(L);}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

8.执行 控制台 可以看到

hello lua on ios
0 0
原创粉丝点击