vs c++使用lua

来源:互联网 发布:北京谷歌seo 编辑:程序博客网 时间:2024/06/03 22:22

一、下载lua,我下载的是lua.5-3-4

二、编译生成lua.lib文件

使用vs创建静态库项目,添加文件(加压后有个src,添加下面的所有文件除了lua.c与luac.c)。

然后运行。但是会报错,例如:什么什么文件版本过早之类的。右击相应的文件,“属性”---》“预编译头”---》“预编译头”,选择“不使用预编译头”。

再运行,就会编译成功,生成lua.lib文件

三、使用lua

在vs应用程序项目中使用lua

在源文件添加lua.lib文件

//1.创建Lua状态  
lua_State *L = luaL_newstate();
if (L == NULL)
{
return 0;
}


//2.加载Lua文件  
int bRet = luaL_loadfile(L, "hh.lua");
if (bRet)
{
cout << "load file error" << endl;
return 0;
}
//3.运行Lua文件  
bRet = lua_pcall(L, 0, 0, 0);
if (bRet)
{
cout << "pcall error" << endl;
return 0;
}
//4.读取变量 
lua_getglobal(L, "str");
string str = lua_tostring(L, -1);
cout << "str = " << str.c_str() << endl;


lua_close(L);

成功输出结果   str = "I am so cool" 



原创粉丝点击