lua中调用C++函数
2024-08-31 14:27:27
lua中调用C++函数
我们产品中提供了很多lua-C API给用户在lua中调用,之前一直没用深究其实现原理,只是根据已有的代码在编码。显然这不是一个好的习惯,没用达到知其所以然的目的。
一、基本原理
将C++函数编译成动态链接库,然后在lua中require,通过下面的示例进行详解。
#ifdef __cplusplus
extern "C" {
#endif
#include<lua.h>
#include<lualib.h>
#include<lauxlib.h>
#ifdef __cplusplus
}
#endif
static int add (lua_State *L)
#ifdef __cplusplus
extern "C" {
#endif
#include<lua.h>
#include<lualib.h>
#include<lauxlib.h>
#ifdef __cplusplus
}
#endif
/**
要写一个lua中可以调用的函数,一般需要三个步骤,
第一步:定义函数,而且要遵守一定的规则,即
参数只能有一个(lua_State*),返回值也只能为
整型int,本例中static关键字可以不写。
*/
static int add (lua_State *L)
{
double op1 = lua_tonumber(L, -1);
double op2 = lua_tonumber(L, -2);
lua_pushnumber(L, op1+op2);
return 1;
}
/**
第二步:列出所有的本模块中供lua调用的函数,为下
面的注册做准备
*/
static luaL_Reg myfuncs[] = {
{"add", add},
{NULL, NULL}
};
/**
第三步:注册函数到一个lua table中去。
*/
extern "C" int luaopen_mytestlib(lua_State *L)
{
luaL_register(L, "mytestlib", myfuncs);
return 1;
}
二、编译
$ g++ lua_call_cplusplus_function.cc -o mytestlib.so -shared -fPIC -I /usr/include/lua5.1/
三、运行结果
$ lua
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> require("mytestlib")
> print(mytestlib.add(1,2))
3
四、注意事项
- luaL_register在lua5.2中被移除了,如果使用lua5.3编译会报如下错误
$ g++ mytestlib.cc -o test.so -shared -fPIC -I /home/wuman/下载/lua-5.3.2/src
mytestlib.cc: In function ‘int luaopen_test(lua_State*)’:
mytestlib.cc:21:34: error: ‘luaL_register’ was not declared in this scope
luaL_register(L, "test", testlib);
- luaopen_mytestlib这个 extern "C"是必须的,否则require时会报错:
$ lua
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> require("mytestlib")
error loading module 'mytestlib' from file './mytestlib.so':
./mytestlib.so: undefined symbol: luaopen_mytestlib
stack traceback:
[C]: ?
[C]: in function 'require'
stdin:1: in main chunk
[C]: ?
最新文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
- 外接Hdmi没有声音怎么设置
- JS 中 Array.slice() 和 Array.splice()方法
- mysql集群数据一致性校验
- lecture11-hopfiled网络与玻尔兹曼机
- python调用ggsci.exe程序
- jquery validate ajax submit form
- Delphi单元文件之-防止程序重复执行
- Unity3D脚本中文系列教程(十七)
- centos 6.5安装esx sdk
- Decorators and Wrappers in Python
- 菜鸟学习SSH(二)——Struts国际化
- JAVA web四个属性的范围汇总
- 百度地图SDK for Android v2.1.3全新发布
- 安卓---apk反编译
- 浙大pat1042题解
- MySQL学习笔记(一)&mdash;数据库基础
- Devops step by step
- 本机是wifi,虚拟机无法连接外网问题
- 简介 - PMP(Project Management Professional)