C++程序
void main()
{
lua_State *L = luaL_newstate();
if (L == NULL)
{
return 1;
}
luaL_openlibs(L);
int ret = luaL_dofile(L, "main.lua");
printf("top = %d \n", lua_gettop(L));
lua_getglobal(L,"event");//獲取函數名
lua_pushstring(L,"key");//壓入一個值
if (lua_pcall(L, 1, 0, 0) != 0)//參數數量,函數返回值,錯誤輸出函數(lua的函數,地址)
{
printf("c++ call lua function error %s\n",lua_tostring(L,-1));//返回錯誤,將錯誤壓入到棧頂
lua_pop(L,1);//若有錯誤則彈出
}
printf("top = %d \n", lua_gettop(L));
}
lua程序:
function event(q)
print("c++ call lua function event")
print("q = "..q)
end
運行結果: