C++ 调用 Lua
直接上代码:
1:c++代码
#include <lua.hpp>
#include <LuaBridge/LuaBridge.h> #include <iostream>
#include <string> class A
{
public:
void action()
{
std::cout<<"Hello I am A\n";
} virtual void doPrint(int a,int b)
{
std::cout<<"in A a:"<<a<<"b:"<<b<<std::endl;
} std::string goodMan() const
{
return "goodman";
}
}; class B : public A
{
public:
void hello(const std::string& info) const
{
std::cout<<"hello:"<<info<<std::endl;
} virtual void doPrint(int a, int b) override
{
std::cout<<"in B just"<<(a + b) <<std::endl;
}
}; void globalFunction()
{
std::cout<<"hello this is a global func\n";
} bool reloadLuaScript(lua_State* L, const std::string& luafile)
{
int state = luaL_dofile(L, luafile.c_str());
if(state != LUA_OK)
{
return false;
}
return true;
} void registerClassAndFunctions(lua_State* L)
{
using namespace luabridge;
//注册全局函数和类函数
getGlobalNamespace(L).addFunction("gloabalFunction",globalFunction);
getGlobalNamespace(L)
.beginClass<A>("A")
.addFunction("action",&A::action)
.addFunction("doPrint", &A::doPrint)
.addFunction("goodMan", &A::goodMan)
.endClass()
.deriveClass<B,A>("B")
.addFunction("hello", &B::hello)
.endClass();
} void testCallLua(lua_State* L)
{
A a;
lua_getglobal(L,"testA");
luabridge::push(L, &a);
lua_pcall(L,1,0,0);
} int main(int argc, char** argv)
{
lua_State* L = luaL_newstate(); luaL_openlibs(L);
std::cout<<"try load file "<<argv[1]<<std::endl; auto ok = reloadLuaScript(L, argv[1]);
if(!ok)
{
std::cout<<"load lua file failed\n";
}
else
{
registerClassAndFunctions(L);
testCallLua(L);
}
lua_close(L);
L = nullptr;
}
2:Lua代码
--print("hello");
--[[
this is note
这个是多行注释
--]]
print("This is myWorld!\n");
function testA(a)
a:action();
a:doPrint(1,2);
end
~
~
3:编译运行
[root@10-120-10-106 NewChart]# g++ -std=c++11 -o testlua testLua.cpp -llua -ldl
[root@10-120-10-106 NewChart]# ./testlua abc.lua
try load file abc.lua
This is myWorld! Hello I am A
in A a:1b:2
C++ 调用 Lua的更多相关文章
- c++调用lua
我们主要解决如下几个问题: 转:http://www.cnblogs.com/zisou/p/cocos2dx-lua2.html http://www.cnblogs.com/sevenyuan/p ...
- vs如何在C++中调用Lua
最近Cocos2dx的学习卡壳了,一般的照抄代码我不想写上来,但想示例也想得我头晕...为了放松大脑调整状态于是开始学习Lua.Lua的语法学习还是比较简单的,学过javascript或者vbscri ...
- vs2013如何在C++中调用Lua(二)
Lua学习笔记 vs2013如何在C++中调用Lua (此为转载教程) 本人试过完全可行 一.准备工作 1.下载Lua源码,地址:http://www.lua.org/download.html(我用 ...
- C中调用Lua函数
我们先来看一个简单的例子: lua_State* L = NULL; // 内部调用lua函数 double f(double x, double y) { double z; lua_getglob ...
- cocos2dx之C++调用Lua
原文地址:http://blog.csdn.net/dingkun520wy/article/details/49839701 1.引入头文件 #include "cocos2d.h&quo ...
- C语言中调用Lua
C语言和Lua天生有两大隔阂: 一.C语言是静态数据类型,Lua是动态数据类型 二.C语言需要程序员管理内存,Lua自动管理内存 为了跨越世俗走到一起,肯定需要解决方案. 解决第一点看上去比较容易,C ...
- VC和VS调用Lua设置以及Lua C API使用。
通过c++调用lua 脚本, 环境VC++6.0 lua sdk 5.1.4 在调用前先认识几个函数.1.调用lua_open()将创建一个指向Lua解释器的指针.2. luaL_ope ...
- Java调用Lua脚本(LuaJava使用、安装及Linux安装编译)
依赖包(附件有下载): 包名 类型 操作系统 luajava-1.1.jar jar ALL libluajava-1.1.so .so linux luajava-1.1.dll .dll wind ...
- Java调用Lua(转)
Java 调用 Lua app发版成本高,覆盖速度慢,覆盖率页低.一些策略上的东西如果能够从服务端控制会方便一些.所以考虑使用Lua这种嵌入式语言作为策略实现,Java则是宿主语言. 总体上看是一个模 ...
- 简述C/C++调用lua中实现的自定义函数
1.首先说下目的,为什么要这么做 ? 正式项目中,希望主程序尽量不做修改,于是使用C/C++完成功能的主干(即不需要经常变动的部分)用lua这类轻量级的解释性语言实现一些存在不确定性的功能逻辑:所以, ...
随机推荐
- Cobbler自动装机--1
cobbler介绍 cobbler官网:http://cobbler.github.io/用个人的话来说就是cobbler就是一款通过网络快速安装Linux操作系统的产品.cobbler可以配置,管理 ...
- php Call to undefined function imagettftext()问题解决
测试代码出现报错Call to undefined function imagettftext(),发现是gd库出现了问题 通过phpInfo()查看 gd库已经开启,但是里边没有freeType 和 ...
- spring-IOC容器(一)
ApplicationContext 代表IOC容器(控制反转) ApplicationContext的主要实现类: ——ClassPathXmlApplicationContext:从类路径下加载配 ...
- NET设计模式 第二部分 结构性模式(10):组合模式(Composite Pattern)
组合模式(Composite Pattern) ——.NET设计模式系列之十一 Terrylee,2006年3月 概述 组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复 ...
- Hive HiveServer2+beeline+jdbc客户端访问操作
HiveServer 查看/home/hadoop/bigdatasoftware/apache-hive-0.13.1-bin/bin目录文件,其中有hiveserver2 启动hiveserver ...
- java面试题001
1.指针和函数的关系 这里主要谈指针函数和函数指针,在c中指针函数是返回值为指针的函数:函数指针是指向函数的指针变量. 2.什么是事务? 为了完成对数据的操作,要求并发访问在多个构件之间共享的数据.这 ...
- SPI核软件调试结果
SPI核软件调试结果 一.硬件搭建 配置如下: 1.采用手动复位: 2.输入时钟27M,AXI总线工作频率100M: 3.axi_quad_spi 配置为标准模式: 4.配合软件例程的使用,挂载了CP ...
- 在Win32程序中显示Dos调试窗口
在很多程序中,都可以看到程序运行中,会有一个Dos窗口,实时显示一些运行信息,这里就告诉大家是如何实现的,我们做个简单的,其实对控制台的操作还有很多,有兴趣的可以去查资料. 用到的API函数如下: / ...
- 测试教程网.unittest教程.1. 基本概念
From:http://www.testclass.net/pyunit/basic_concept/ unittest是python自带的单元测试框架,有时候又被称为”PyUnit”,是python ...
- centos 7 免密登录
本文转载自:https://www.cnblogs.com/hobinly/p/6039844.html 环境示例 Centos7 192.168.1.101 master Centos7 192. ...