在c++中调用lua的函数

在编写下面代码前首先你的需要#include "lua.h" #include "lualib.h" #include "lauxlib.h"头文件

lua51.lib lua51.dll 这两个库文件


#include <stdio.h>

#include <string.h>
extern "C"{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
#pragma comment(lib, "lua51.lib")//这里是lua的库文件
extern "C" int _Version(lua_State *L)
{
puts("This is Version 1.0 of the Console program");
puts(LUA_VERSION);
puts(LUA_COPYRIGHT);
puts(LUA_AUTHORS);
return 0;
}


static luaL_reg consoleGlua[]=
{
{"Version",_Version},
{NULL,NULL}
};
char gpCommandBuffer[256];
const char *gpCommand(void)
{
printf("Ready> ");
return gets(gpCommandBuffer);
puts("\n");
};


int main()
{
char buff[256];
int error;
lua_State *pLuaState = lua_open();
luaopen_base(pLuaState);
luaopen_base(pLuaState); /* opens the basic library */
luaopen_table(pLuaState); /* opens the table library */
//luaopen_io(pLuaState); /* opens the I/O library */这里一般不开启
luaopen_string(pLuaState); /* opens the string lib. */
luaopen_math(pLuaState); /* opens the math lib. */


luaL_dofile(pLuaState,"test.lua");//加载lua文件
lua_getglobal(pLuaState,"aa");//查找需要调用的函数
lua_pcall(pLuaState,0,0,0);//调用函数
lua_getglobal(pLuaState,"bb");
lua_pcall(pLuaState,0,0,0);


lua_close(pLuaState);//关闭lua文件
getchar();
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值