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)
運行結果: