int init_lua (char *filename,lua_State *L){
//int inter;
//lua_State *L = luaL_newstate();
luaL_openlibs(L);
if(luaL_loadfile(L, filename) ){
error(L, "cannot run configuration file: %s",lua_tostring(L, -1));
return 1;
}
if (lua_pcall(L, 0, 0, 0)){
error(L, "lua_pcall() failed");
return 2;
}
return 0;
}
uint8_t get_interface(unsigned int inteiro,lua_State *L){
int inter;
lua_getglobal(L,"ret_ind"); //função a ser chamada
lua_pushnumber(L,inteiro); //argumento
// do the call (1 arguments, 1 result)
if (lua_pcall(L, 1, 1, 0))
error(L, "error running function 'f': %s",lua_tostring(L, -1));
if (!lua_isnumber(L, -1))
error(L, "function 'f' must return a number");
inter = lua_tonumber(L, -1);
lua_pop(L, 1); // pop returned value /
return (uint8_t)inter;
}
int main(void)
{
lua_State *L = luaL_newstate();
printf("apos o load3\n");
if (init_lua("pp.lua",L))
printf("erro ao tentar inicializar arquivo lua\n");
uint8_t resul;
resul = get_interface(190,L);
printf("Index of array: %d\n",(int)resul);
resul = get_interface(10000,L);
printf("Index of array: %d\n",(int)resul);
return 0;
}
The code above as pp.c
interfaces = {190,3141592,10000}
function ret_ind (ip)
for i, inter in ipairs(interfaces) do
if inter == ip then
return i
end
end
return 0;
end
The code above as pp.lua
and the command to compile gcc -o pp pp.c -I/usr/include/lua5.1/ -llua5.1
I'm trying to include the source of lua so I don't need have the lua installed on the machine. with the below I can't compile once I remove the the liblua5.1-0-dev from the system.
gcc -std=c99 -O2 -I./lua5.1/src/ pp.c -o pp.o -llua5.1
with the library on the system I can use local source but global lua5.1 from -llua5.1.
EDIT
After transform the lua source code into shered object (.so) I got a executing program with: OBS: I will put the makefile I used as a comment of this post
gcc -o pp pp.c -I./lua5.1/src/ -L./lua5.1/src/ -llua5.1 to execute the program I use (must execute the 2 follows command so the ld can find the .so file)
LD_LIBRARY_PATH=./lua5.1/src/ export LD_LIBRARY_PATH and ran ./pp