this is the code, when execute get the error:"PANIC: unprotected error in call to Lua API (attempt to call a nil value)"
#include <stdio.h>
extern "C"{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
lua_State *L;
int luaAdd(int x, int y)
{
int sum;
lua_getglobal(L, "add");
lua_pushnumber(L, x);
lua_pushnumber(L, y);
lua_call(L, 2, 1);
sum = (int)lua_tonumber(L, -1);
lua_pop(L, 1);
return sum;
}
int main(int argc, char *argv[])
{
int sum = 0;
L = lua_open();
luaL_openlibs(L);
luaL_dofile(L, "add.lua");
sum = luaAdd(10, 15);
printf("The sum is %d\n", sum);
lua_close(L);
return 0;
}
add.lua
function add(x, y) do
return x + y
end
end
can you tell me ,where am i wrong. thanks in advance.