对于Lua 5.2, lua 5.2是2011年发布的。国内使用5.1的居多。
luaL_register 和luaL_openlibs。这些功能大多数都废弃了
luaL_register这个注册c++的库函数,功能被废弃了。新的api luaL_newlib 网上的资料不多。
下面给出一个使用 luaL_newlib的例子来调用c++ 函数
//this is c++ code
#include "stdafx.h"
#include "math.h"
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
};
//this is callback function for the lua
static int GetSin(lua_State* luaState)
{
float f;
f = (float)luaL_checknumber(luaState, 1);
// f = (float)lua_tonumber(luaState,1);
float v = sinf(f);
lua_pushnumber(luaState,v);
return 1;
}
static const luaL_Reg testlib[] =
{
{
"GetSin",GetSin},
{
NULL,NULL
}
};
int luaopen
GetLib(lua_State* L)
{
luaL_newlib(L,testlib);
return 1; //return one value
}
int main(int argc, _TCHAR* argv[])
{
lua_State *L = luaL_newstate();
luaL_requiref(L,"sinlib",luaopen
GetLib,1);
int status = luaL_loadfile(L,"cfg.lua"); //load the cfg.lua
lua_pcall(L, 0, 0, 0); //execute the loaded cfg.lua
int top = lua_gettop(L);
double x = 10;
double y = 20;
double z = 0;
lua_getglobal(L, "foo");
bool isf = lua_isfunction(L,-1);
lua_pushnumber(L, x);
lua_pushnumber(L, y);
//execute the lua function (pass two parameters, return one result
if (lua_pcall(L, 2, 1, 0) != 0)
luaL_error(L, "error running function 'f': %s",
lua_tostring(L, -1));
if (!lua_isnumber(L, -1))
luaL_error(L, "function 'foo' must return a number");
z = lua_tonumber(L, -1);
lua_pop(L, 1);
lua_close(L);
return 0;
}
cfg.lua的代码在这里
function foo(x, y)
end