C++程序:
int main()
{
lua_State *L = luaL_newstate();
if (L == NULL)
{
return 1;
}
luaL_openlibs(L);
//向lua传递表
lua_newtable(L);//创建一个表
lua_pushstring(L, "name");//压入key
lua_pushstring(L, "xiaoming");//压入value
lua_settable(L, -3);//写入表中
lua_pushstring(L, "age");
lua_pushinteger(L, 20);
lua_settable(L, -3);
lua_setglobal(L, "student");//将栈顶元素设置全局变量
int ret = luaL_dofile(L, "main.lua");
if (ret)
{
printf("Lua doFile Error !\n");
}
}
lua程序:
print("student name :"..student["name"])
print("student age :"..student.age)
运行结果: