在lua中调用C++
来源:互联网 发布:网络剧有毒 原著 编辑:程序博客网 时间:2024/06/05 20:03
#include "stdafx.h"#include <iostream> #include <luabind/luabind.hpp>using namespace std;void print_hello(int number) { std::cout << "hello world " << number << std::endl;}int _tmain(int argc, _TCHAR* argv[]){lua_State *L = ::luaL_newstate(); /* opens Lua */luabind::open(L);luabind::module(L) [ luabind::def("print_hello", print_hello) ]; // 现在Lua中可以调用print_hello了 luaL_dostring(L, "print_hello(123)"); lua_close(L); }
1.导出C++函数
先还要把C++函数添加到lua环境,然后在C++中调用lua函数,被调用的lua函数中再调用C++函数。这好像是一个循环,但其实在项目中可以使用lua来组织功能。
#include <iostream> #include <luabind/luabind.hpp>using namespace std; class NumberPrinter { public: NumberPrinter(int number) : m_number(number) {} void print() { std::cout << m_number << std::endl; } private: int m_number;};int _tmain(int argc, _TCHAR* argv[]){lua_State *L = ::luaL_newstate(); /* opens Lua */luabind::open(L);luabind::module(L)[ luabind::class_<NumberPrinter>("NumberPrinter") .def(luabind::constructor<int>()) .def("print",&NumberPrinter::print) ]; // 现在Lua中可以使用NumberPinter类了 luaL_dostring(L,"Print2000 = NumberPrinter(2000) ""Print2000:print() " ); lua_close(L); }
2.导出C++类
参考网站:点击打开链接,点击打开链接
0 0
- Lua 在Lua中调用C函数
- 在Lua中调用C函数
- 在C中调用Lua代码
- Ubuntu下安装Lua以及在C中调用Lua
- Lua 在C程序中调用Lua函数
- lua中调用C
- LUA中调用C
- 在LUA脚本中调用C/C++函数
- 在C中调用Lua作配置文件和响应函数
- 如何在c/c++中调用LUA函数
- 在c中调用lua函数,获取返回值等
- 在C中调用lua的内置函数string.gsub
- 在c中调用lua函数,获取返回值等
- 在Lua代码中调用c/C++函数
- cocos2dx关于在c/c++中调用lua函数
- 在Lua代码中调用c/C++函数
- 在C/C++中调用LUA脚本简介
- C/C++中调用Lua
- LeetCode 开篇
- Centos下编译mysql5.6.16
- JS 实现MD5加密,以及发现的JS对字符的处理、替换等
- HDU 2289 Cup.
- a different object with the same identifier value was already associated with the session
- 在lua中调用C++
- 黑马程序员_JAVA反射学习笔记
- 遥想那一抹绿色
- linux下创建和删除软、硬链接
- Tomcat 系统架构与设计模式,第 2 部分: 设计模式分析
- VS开发环境常见编译错误——LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- header WWW-Authenticate认证
- 电源管理之android内核suspend to disk的实现(二)
- 应对挑战 戴尔R920为苛刻工作负载而生