当前位置: 首页 > news >正文

C++调用lua脚本,包括全局函数绑定、类绑定,十分钟快速掌握

系列文章目录

lua调用C/C++的函数,十分钟快速掌握


C++调用lua脚本,包括全局函数绑定、类绑定,十分钟快速掌握

  • 系列文章目录
  • 摘要
  • 环境
  • 使用步骤
  • 码代码
    • 自定义函数
      • 多返回值
      • 变长参数
    • 自定义类
    • test_sol2.lua内容
  • 程序输出


摘要

在这个快节奏的技术博客中,我们将借助sol2库,以十分钟的时间快速掌握如何在C++中调用Lua脚本。sol2是一个轻量级的Lua绑定库,它简化了C++与Lua之间的交互,使得全局函数和类的绑定变得异常简便。我们将创建几个”复杂的“函数,变长参数、多返回值,和一个具有几个成员函数的简单类,通过sol2的简洁语法,只需几行代码,我们就能将C++函数绑定到Lua环境中,实现跨语言调用。

为了快速输出我们要讲的内容,我不仅提供了实际的示例代码,还将解释每一步的操作,确保能够迅速理解并运用这一技术。通过这十分钟的快速掌握,希望将能够在项目中充分发挥C++与Lua的优势,实现更灵活、高效的代码编写。无论是全局函数的调用还是类的绑定,sol2为C++与Lua的融合提供了一种极具便利性和效率的解决方案。

环境

工具\组件版本说明
Windows10
visual studio2022
lua5.4
sol23.2.3
C++17sol2需要

使用步骤

lua头文件和静态链接库或者动态库,从官方网站下载预编译好的即可,当然如果你有自定义需求也可自行修改编译。

引入sol2头文件,从github上下载。

# 就是这三个文件,要按此层次结构放好。
│  sol.hpp
│
└─solconfig.hppforward.hpp

visual studio新建个项目,C++语言标准设置为C++17。

加入头文件和库文件到项目中。

接下来就是码代码了。


码代码

自定义函数

多返回值

通过使用sol2我们不需要自己再写包装方法,这一下就省了不少代码。

std::tuple<int,const char*, double> multipleResultFunc(int a, const char* b, double c)
{//return std::tuple<int, char*, double>(a, b, c);return std::make_tuple(a, b, c);
}

变长参数

int variaArgs(sol::variadic_args va)
{int r = 0;for (auto v : va){r += static_cast<int>( v);}return r;
}

自定义类

	class my_class{private:std::string m_str;public:int b = 24;int f() const{return 24;}void g(){++b;}void setTitle(const char* str){this->m_str = str;}void print(){std::cout << this->m_str << " , " << this->b << std::endl;std::wcout << this->rowname << L" , " << typeid( this->columns).name() << std::endl;}}
#define SOL_CHECK_ARGUMENTS 1
#include "sol.hpp"sol::state lua;lua.open_libraries(sol::lib::base);// "bark" namespacing in Lua// namespacing is just putting things in a tablesol::table bark = lua.create_named_table("bark");bark.new_usertype<my_class>("my_class","f", &my_class::f,"g", &my_class::g,"setTitle", &my_class::setTitle,"print", &my_class::print//,//"putUnsetValue", &my_class::putUnsetValue//,//"setSome", &my_class::setSome); // the usuallua.set_function("multipleResultFunc", multipleResultFunc);lua.set_function("variaArgs", variaArgs);// load and execute from filelua.script_file("test_sol2.lua");

test_sol2.lua内容

obj = bark.my_class.new()
obj:g()
local result =obj:f()
obj:setTitle("I am Tom , and you ?")
print(">>>>>>")
obj:print()bark.print_my_class(obj)
print(result)print(bark.add_two_number(1,2))mr, mr2, mr3 = multipleResultFunc(11, "Jerry", 123.45)
print(mr, mr2, mr3)
print(mr)print("call variable args function", 1, 2, 3, 4)
local r= variaArgs(1,2,3,4)
print(variaArgs, r)

程序输出

=== namespacing ===
I am Tom , and you ? , 25, class std::vector<unsigned long,class std::allocator<unsigned long> >
my_class { b: 25 }
24
3
11      Jerry   123.45
11
call variable args function     1       2       3       4
function: 0102B030      10
C source output Your name is Michael, and 2323 years old.
http://www.lryc.cn/news/229947.html

相关文章:

  • 快乐数[简单]
  • Spring源码阅读-ClassPathXmlApplicationContext
  • 考研分享第2期 | 中央财经大学管理科学跨考北大软微金融科技406分经验分享
  • Linux安装java jdk配置环境 方便查询
  • 惊群效应之Nginx处理
  • SpringBoot整合Ldap--超详细方法讲解
  • 【工程实践】Docker使用记录
  • FreeSwitch安装视频
  • SpringBoot3+Vue3+Mysql+Element Plus完成数据库存储blob类型图片,前端渲染后端传来的base64类型图片
  • 攻略 | 参与Moonbeam Ignite Ecosystem Tour
  • 【python自动化】Playwright基础教程(七)Keyboard键盘
  • Java读取文件内容写入新文件
  • 学习samba
  • 【Ansible】Ansible的Ad-hoc命令执行流程
  • Postgresql 常用整理
  • 如何在Jupyter Lab中安装不同的Kernel
  • Java钩子函数的使用
  • C++跨DLL内存所有权问题探幽(一)DLL提供的全局单例模式
  • 短时间不点击云服务器,自动化断开连接,怎么设置长时间
  • typhonjs-escomplex 代码可读性 可维护度探索
  • 支持向量机基本原理,Libsvm工具箱详细介绍,基于支持向量机SVM的人脸朝向识别
  • 密码破解工具的编写
  • BES2700H开发不完全手册
  • OpenGL的学习之路-3
  • Vue 小黑记事本组件版
  • javascript如何清空数组?
  • MySQL MHA高可用切换
  • 【Python】【应用】Python应用之一行命令搭建http、ftp服务器
  • C++模拟实现——红黑树
  • java基础-数据类型