extern "C"{
#include "src/lualib.h"
#include "src/lauxlib.h"
#include "src/lua.h"
}
#include "iostream"
using namespace std;
int get_sum(lua_State *l)
{
int n = lua_gettop(l);/*获得lua调用时参数个数*/
double sum = 0;
for (int i = 1; i <= n; i++)
{
if (lua_isnumber(l, i))
{
sum += lua_tonumber(l, i);/*获得参数*/
}
else
{
lua_pushstring(l, "error not a number");
return 1;
}
}
lua_pushnumber(l, sum);/*返回给lua*/
return 1;/*返回 返回值个数*/
}
int main()
{
lua_State *l = lua_open();
luaL_openlibs(l);
lua_register(l, "get_sum", get_sum);
luaL_dofile(l, "a.lua");
lua_close(l);
system("pause");
return 0;
}
print("sum is "..get_sum(1,2,"3"))