#include <iostream>
extern "C" {
#include <lua.h>
#include <luaxlib.h>
#include <lualib.h>
}
int main()
{
lua_State * lua = luaL_newstate();
luaL_openlibs(lua);
luaopen_base(lua);
if(luaL_loadfile(lua,"main.lua"))
{
const std::string error = lua_tostring(lua,-1);
std::cout << "lua file load error:" << error << std::endl;
return -1;
}
if(lua_pcall(lua,0,0,0))
{
const std::string error = lua_tostring(lua,-1);
std::cout << "lua pcall error:" << error << std::endl;
return -1;
}
//获取全局变量
lua_getgloble(lua,"width");
int width = lua_tonumber(lua,-1);
lua_getgloble(lua,"heigth");
int height = lua_tonumber(lua,-1);
lua_pop(lua,1);
std::cout << "width = " << width << std::endl;
std::cout << "height = " << height << std:: endl;
lua_close(lua);
return 0;
}