c++ 调用lua函数简单案例

2 篇文章 0 订阅

主要利用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++

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值