lua调用C++函数

博客介绍了Lua与C++的调用方式,主要包含静态方式和动态库方式,为相关开发提供了不同的实现思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 静态方式

#ifndef _DLL_MODULE_LUA_GLUE_HPP_
#define _DLL_MODULE_LUA_GLUE_HPP_

//  加载lua头文件
#include "lua.hpp"

// 第一步:定义luaGlue函数
int add(lua_State* L)
{
	double op1 = lua_tonumber(L, -1);
	double op2 = lua_tonumber(L, -2);
	lua_pushnumber(L, op1 + op2);
	return 1;
}

int sub(lua_State* L)
{
	double op1 = lua_tonumber(L, -1);
	double op2 = lua_tonumber(L, -2);
	lua_pushnumber(L, op1 - op2);
	return 1;
}

// 第二步:注册函数到lua_table中
extern "C" int lua_regist_function(lua_State* L)
{
	// 静态库
	lua_register(L, "cpp_add", add);
	lua_register(L, "cpp_sub", sub);
	return 1;
}

// 第三步:在主函数中,创建并注册luaGlue函数

int main ( int argc, char *argv[] )  
{  
  
	lua_State *L = luaL_newstate();
	if (L == nullptr)
	{
		return -1;
	}

	int bRet = luaL_loadfile(L, "./test.lua");
	if (bRet)
	{
		cout << "load test.lua file failed" << endl;
		return -1;
	}

	bRet = lua_pcall(L, 0, 0, 0);
	if (bRet)
	{
		cout << "call test.lua file failed" << endl;
		return -1;
	}
  	//////////////////////////////////////////////////////////////////////////
	// 注册luaGlue函数
	lua_regist_function(L);
	//////////////////////////////////////////////////////////////////////////
	// 测试从c++调用lua函数,然后lua内部再调用c++函数
	
	lua_getglobal(L, "add");
	// 将参数压入栈
	lua_pushnumber(L, 5);
	lua_pushnumber(L, 5);
	iRet = lua_pcall(L, 2, 1, 0);
	if (iRet)
	{
		const char* pErrorMsg = lua_tostring(L, -1);
		cout << pErrorMsg << endl;
		lua_close(L);
		return -1;
	}
	// 从栈顶获取结果值
	if (lua_isnumber(L, -1))
	{
		double dRet = lua_tonumber(L, -1);
		cout << "c++ -> lua -> c++ :" << dRet<<endl;
	}
	//////////////////////////////////////////////////////////////////////////
	
	lua_close(L);
	// waiting for console
	cin.get();
	return 0;
	}
	#endif
	// 第四步:在lua中调用 cpp_add,cpp_sub直接操作
	// test. lua代码
	function add(a, b)
		a = a * a
		b = b * b
		return cpp_add(a, b)
	end

2. 动态库方式

#ifndef _DLL_MODULE_LUA_GLUE_HPP_
#define _DLL_MODULE_LUA_GLUE_HPP_

#include "lua.hpp"

// 第一步:定义luaGlue函数
int add(lua_State* L)
{
	double op1 = lua_tonumber(L, -1);
	double op2 = lua_tonumber(L, -2);
	lua_pushnumber(L, op1 + op2);
	return 1;
}

int sub(lua_State* L)
{
	double op1 = lua_tonumber(L, -1);
	double op2 = lua_tonumber(L, -2);
	lua_pushnumber(L, op1 - op2);
	return 1;
}

// 第二步:列出本模块需要被lua调用的函数,以NULL结尾
luaL_Reg luaFunctions[] = {
	{ "cpp_add", add },
	{ "cpp_sub", sub },
	{ NULL, NULL}
};


// 第三步:注册函数到lua_table中
extern "C" int lua_regist_function(lua_State* L)
{
	// 动态库
	luaL_newlib(L, luaFunctions);
	return 1;
}

// 第四步:在主函数中,创建并注册luaGlue函数
int main ( int argc, char *argv[] )  
{  
  
	lua_State *L = luaL_newstate();
	if (L == nullptr)
	{
		return -1;
	}

	int bRet = luaL_loadfile(L, "./test.lua");
	if (bRet)
	{
		cout << "load test.lua file failed" << endl;
		return -1;
	}

	bRet = lua_pcall(L, 0, 0, 0);
	if (bRet)
	{
		cout << "call test.lua file failed" << endl;
		return -1;
	}
  	//////////////////////////////////////////////////////////////////////////
	// 注册luaGlue函数
	lua_regist_function(L);
	//////////////////////////////////////////////////////////////////////////
	// 测试从c++调用lua函数,然后lua内部再调用c++函数
	
	lua_getglobal(L, "add");
	// 将参数压入栈
	lua_pushnumber(L, 5);
	lua_pushnumber(L, 5);
	iRet = lua_pcall(L, 2, 1, 0);
	if (iRet)
	{
		const char* pErrorMsg = lua_tostring(L, -1);
		cout << pErrorMsg << endl;
		lua_close(L);
		return -1;
	}
	// 从栈顶获取结果值
	if (lua_isnumber(L, -1))
	{
		double dRet = lua_tonumber(L, -1);
		cout << "c++ -> lua -> c++ :" << dRet<<endl;
	}
	//////////////////////////////////////////////////////////////////////////
	
	lua_close(L);
	// waiting for console
	cin.get();
	return 0;
	}
	
#endif
// 第四步:在lua中调用 require("luaFunctions") 加载luaFunctions模块, 然后调用luaFunctions.add(1, 2) 调用add函数
//test.lua 代码
local lua_func = require("luaFunctions")
function add(a, b)
	a = a * a
	b = b * b
	return lua_func.cpp_add(a, b)
end
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值