C++调用lua

C++ 调用lua,下面是一个简单的例子更多请看参考。

我使用的是lua 5.3版本。lua 5.2 去掉了luaL_register功能,也就没弄lua调用C++函数。这个有时间在搞一下

参考: http://www.jellythink.com/archives/882 

用vs2013去搭建编译lua的工程稍微麻烦,我就选用了开源的codeblocks来调试整个工程。

新建一个console application,然后将lua 5.3的源码放到lua53目录下,main.cpp在src目录下。




下面的例子,是创建一个新的lua文件,并将代码写入到lua文件中。然后再从c++中读取这个lua文件,执行lua中的函数。

#include <iostream>
#include <string.h>
#include <fstream>
extern "C"
{
    #include "../lua53/lua.h"
    #include "../lua53/lauxlib.h"
    #include "../lua53/lualib.h"
}

using namespace std;

void writeCfg()
{
    ofstream examplefile("config.lua");
    if (examplefile.is_open())
    {
        examplefile << "width = 200\n";
        examplefile << "height = 3203\n";
        examplefile << "function add(a, b)\n";
        examplefile << "\treturn a+b\n";
        examplefile << "end\n";
        examplefile.close();
    }
}

// C++ call lua
int loadCfg()
{
    lua_State *L = luaL_newstate();
    if(!L) return -1;
    int bRet = luaL_loadfile(L, "config.lua");
    if( bRet ) return -2;
    bRet = lua_pcall(L, 0,0,0);
    if( bRet ) return -3;
    lua_getglobal(L, "width");
	lua_getglobal(L, "height");

	if (!lua_isnumber(L, -2)) return -4;
	if (!lua_isnumber(L, -1)) return -5;

	int iWindowHeight = lua_tointeger(L, -1);
	int iWindowWidth = lua_tointeger(L, -2);
    cout<<iWindowHeight <<" " << iWindowWidth<<"\n";
    return 1;
}

void loadFunc()
{
    lua_State *L = luaL_newstate();
	if (!L)return ;

	int iRet = luaL_loadfile(L, "config.lua");
	if (iRet)
	{
		const char *pErrorMsg = lua_tostring(L, -1);
		cout << pErrorMsg << endl;
		lua_close(L);
	}

	luaL_openlibs(L);

	iRet = lua_pcall(L, 0, 0, 0);
	if (iRet)
	{
		const char *pErrorMsg = lua_tostring(L, -1);
		cout << pErrorMsg << endl;
		lua_close(L);
		return ;
	}

    lua_getglobal(L, "add"); // 获取函数,压入栈中
    lua_pushnumber(L, 10); // 压入第一个参数
    lua_pushnumber(L, 20); // 压入第二个参数

    // 完成调用
    iRet = lua_pcall(L, 2, 1, 0);
    if (iRet)
    {
        const char *pErrorMsg = lua_tostring(L, -1);
        cout << pErrorMsg << endl;
        lua_close(L);
        return;
    }

    // 获得计算结果
    iRet = lua_isnumber(L, -1);
    if (!iRet)
    {
        cout << "Error occured." << endl;
        lua_close(L);
        return;
    }

    double fValue = lua_tonumber(L, -1);
    cout << "Result is " << fValue << endl;
}

int main()
{
    writeCfg();

	cout<<loadCfg()<<endl;
    loadFunc();
    return 0;
}


main函数可能会有冲突,将lua.c   luac.c 中的main函数改成别的函数名字即可。

工程中的样子



运行结果:

3203 200

1

Result is 30


============================================

http://www.cnblogs.com/sevenyuan/p/4511808.html    这个写的很全面

https://blog.csdn.net/xieyihua1994/article/details/77896267   解释了 在lua52 之后 lua_open 函数,用luaL_newstate来代替

添加一个lua调用c++的例子:

#include <iostream>
#include <string.h>
#include <fstream>

//目录结构稍微调整了下
extern "C"
{
    #include "lua53/lua.h"
    #include "lua53/lauxlib.h"
    #include "lua53/lualib.h"
}

using namespace std;

void writeCfg()
{
    ofstream examplefile("config.lua");
    if (examplefile.is_open())
    {
        examplefile << "width = 200\n";
        examplefile << "height = 3203\n";
        examplefile << "function add(a, b)\n";
        examplefile << "\treturn a+b\n";
        examplefile << "end\n";
        examplefile.close();
    }
}

///
// C++ call lua
int loadCfg()
{
    lua_State *L = luaL_newstate();
    if(!L) return -1;
    int bRet = luaL_loadfile(L, "config.lua");
    if( bRet ) return -2;
    bRet = lua_pcall(L, 0,0,0);
    if( bRet ) return -3;
    lua_getglobal(L, "width");
	lua_getglobal(L, "height");

	if (!lua_isnumber(L, -2)) return -4;
	if (!lua_isnumber(L, -1)) return -5;

	int iWindowHeight = lua_tointeger(L, -1);
	int iWindowWidth = lua_tointeger(L, -2);
    cout<<iWindowHeight <<" " << iWindowWidth<<"\n";
    return 1;
}

void loadFunc()
{
    lua_State *L = luaL_newstate();
	if (!L)return ;

	int iRet = luaL_loadfile(L, "config.lua");
	if (iRet)
	{
		const char *pErrorMsg = lua_tostring(L, -1);
		cout << pErrorMsg << endl;
		lua_close(L);
	}

	luaL_openlibs(L);

	iRet = lua_pcall(L, 0, 0, 0);
	if (iRet)
	{
		const char *pErrorMsg = lua_tostring(L, -1);
		cout << pErrorMsg << endl;
		lua_close(L);
		return ;
	}

    lua_getglobal(L, "add"); // 获取函数,压入栈中
    lua_pushnumber(L, 10); // 压入第一个参数
    lua_pushnumber(L, 20); // 压入第二个参数

    // 完成调用
    iRet = lua_pcall(L, 2, 1, 0);
    if (iRet)
    {
        const char *pErrorMsg = lua_tostring(L, -1);
        cout << pErrorMsg << endl;
        lua_close(L);
        return;
    }

    // 获得计算结果
    iRet = lua_isnumber(L, -1);
    if (!iRet)
    {
        cout << "Error occured." << endl;
        lua_close(L);
        return;
    }

    double fValue = lua_tonumber(L, -1);
    cout << "Result is " << fValue << endl;
}

//
// lua call c++
/* 指向Lua解释器的指针 */
lua_State* L;
static int average(lua_State *L)
{
    /* 得到参数个数 */
    int n = lua_gettop(L);
    double sum = 0;
    int i;

    /* 循环求参数之和 */
    for (i = 1; i <= n; i++)
    {
        /* 求和 */
        sum += lua_tonumber(L, i);
    }
    /* 压入平均值 */
    lua_pushnumber(L, sum / n);
    /* 压入和 */
    lua_pushnumber(L, sum);
    /* 返回返回值的个数 */
    return 2;
}

void luaCallCpp()
{
    /* 初始化Lua */
    L = luaL_newstate(); // lua52 之后,就移除了lua_open 函数,用luaL_newstate来代替

    /* 载入Lua基本库 */
    luaL_openlibs(L);
    /* 注册函数 */
    lua_register(L, "average", average);
    /* 运行脚本 */
    luaL_dofile(L, "avg.lua");
    /* 清除Lua */
    lua_close(L);

}



//
int main()
{
    writeCfg();
	cout<<loadCfg()<<endl;
    loadFunc();

    cout << "==========================="<<endl;
    luaCallCpp();
    return 0;
}

上面的lua代码,基于lua53,


-- avg.lua
avg, sum = average(10, 20, 30, 40, 50)  
print("The average is ", avg)  
print("The sum is ", sum)
-- config.lua 这个文件是自动生成的其实
width = 200
height = 3203
function add(a, b)
	return a+b
end
-- func.lua
function add(a, b)
	return a+b
end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值