主要利用lua的接口函数:
luaL_dostring(L,”“);
luaL_dofile(L);
还有讲lua转化为动态链接库;
使用工具:xcode7.0
上代码案例:
//
// testlua.h
// Test_lua
//
// Created by Intelligent on 16/12/2.
// Copyright © 2016年 com.Intelligent. All rights reserved.
//
#pragma once
#import <Foundation/Foundation.h>
#import <iostream>
#import "string"
#include <lua.hpp>
class testlua
{
public:
testlua();
~testlua();
const char * testprint(const char* CSVFilepath);
private:
lua_State *m_luastate;//定义lua环境变量指针
};
//
// testlua.m
// Test_lua
//
// Created by Intelligent on 16/12/2.
// Copyright © 2016年 com.Intelligent. All rights reserved.
//
#import "testlua.h"
#import <Foundation/Foundation.h>
testlua::testlua()
{
m_luastate = luaL_newstate();
lua_status(m_luastate);
luaL_openlibs(m_luastate);
}
testlua::~testlua()
{
lua_close(m_luastate);
}
const char * testlua::testprint(const char* CSVFilepath)
{
luaL_dofile(m_luastate, "/Users/mac/Documents/程序/test_forlua_int1/Berequired.lua");
luaL_dostring(m_luastate,"print(myprivateFunction()) print(vprint())");
lua_getglobal(m_luastate,"getglobalvalue");
lua_call(m_luastate, 0, 1);
// const char * str = lua_tostring(m_luastate, 1);
const char * str = lua_tostring(m_luastate, lua_gettop(m_luastate));
// std::cout<<str;
puts(str);
lua_getglobal(m_luastate,"getglobalvalue");
lua_call(m_luastate, 0, 1);
str = lua_tostring(m_luastate, lua_gettop(m_luastate));
// std::cout<<str;
puts(str);
return 0;
}
//
// AppDelegate.h
// test_forlua_int1
//
// Created by Intelligent on 17/3/11.
// Copyright © 2017年 com.Intelligent. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#include "testlua.h"
@interface AppDelegate : NSObject <NSApplicationDelegate>
{
@public
testlua *testluaaa;
}
-(IBAction)nsbutton:(id)sender;
@end
//
// AppDelegate.m
// test_forlua_int1
//
// Created by Intelligent on 17/3/11.
// Copyright © 2017年 com.Intelligent. All rights reserved.
//
#import "AppDelegate.h"
@interface AppDelegate ()
@property (weak) IBOutlet NSWindow *window;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
-(IBAction)nsbutton:(id)sender
{
testluaaa = new testlua();
testluaaa->testprint("");
}
@end
注意:添加testlua.h,testlua.m ,加载lua脚本到工程文件目录,添加lua.xcodeproj到当前目录;
编译运行:
0
0
0
0
0
0
0
0
0
0
this is a public function
this is a required package
this is a private funciton
-3
this is a public function
this is a required package
c
c++