0
votes
    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

1
This is a working program.DevBush
What errors do you get? Why don't you statically link Lua instead of grafting the source code to your project?Colonel Thirty Two

1 Answers

0
votes

The answer was found in: Compiling Lua - create .so files? to make liblua5.1.so use make so

The make file used:

# makefile for building Lua
# see ../INSTALL for installation instructions
# see ../Makefile and luaconf.h for further customization

# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================

# Your platform. See PLATS for possible values.
PLAT= none

CC= gcc
CFLAGS= -O2 -Wall $(MYCFLAGS)
AR= ar rcu
RANLIB= ranlib
RM= rm -f
LIBS= -lm $(MYLIBS)

MYCFLAGS=-fPIC
MYLDFLAGS=
MYLIBS=

# == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE =========

PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris

LUA_A=  liblua.a
LUA_SO= liblua5.1.so
CORE_O= lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \
    lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o  \
    lundump.o lvm.o lzio.o
LIB_O=  lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \
    lstrlib.o loadlib.o linit.o

LUA_T=  lua
LUA_O=  lua.o

LUAC_T= luac
LUAC_O= luac.o print.o

ALL_O= $(CORE_O) $(LIB_O) $(LUA_O) $(LUAC_O)
#ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T)
ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) $(LUA_SO) 
ALL_A= $(LUA_A)

default: $(PLAT)

all:    $(ALL_T)

o:  $(ALL_O)

a:  $(ALL_A)

so: $(LUA_SO)

$(LUA_SO): $(CORE_O) $(LIB_O)
    $(CC) -shared -ldl -Wl,-soname,$(LUA_SO) -o $@ $? -lm $(MYLDFLAGS)
    #ln -sf $(LUA_SO).$(R) $(LUA_SO).$(V)
    #ln -sf $(LUA_SO).$(R) $(LUA_SO)

$(LUA_A): $(CORE_O) $(LIB_O)
    $(AR) $@ $(CORE_O) $(LIB_O) # DLL needs all object files
    $(RANLIB) $@

$(LUA_T): $(LUA_O) $(LUA_A)
    $(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(LUA_A) $(LIBS)

$(LUAC_T): $(LUAC_O) $(LUA_A)
    $(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS)

clean:
    $(RM) $(ALL_T) $(ALL_O)

depend:
    @$(CC) $(CFLAGS) -MM l*.c print.c

echo:
    @echo "PLAT = $(PLAT)"
    @echo "CC = $(CC)"
    @echo "CFLAGS = $(CFLAGS)"
    @echo "AR = $(AR)"
    @echo "RANLIB = $(RANLIB)"
    @echo "RM = $(RM)"
    @echo "MYCFLAGS = $(MYCFLAGS)"
    @echo "MYLDFLAGS = $(MYLDFLAGS)"
    @echo "MYLIBS = $(MYLIBS)"

# convenience targets for popular platforms

none:
    @echo "Please choose a platform:"
    @echo "   $(PLATS)"

aix:
    $(MAKE) all CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" MYLIBS="-ldl" MYLDFLAGS="-brtl -bexpall"

ansi:
    $(MAKE) all MYCFLAGS=-DLUA_ANSI

bsd:
    $(MAKE) all MYCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" MYLIBS="-Wl,-E"

freebsd:
    $(MAKE) all MYCFLAGS="-DLUA_USE_LINUX" MYLIBS="-Wl,-E -lreadline"

generic:
    $(MAKE) all MYCFLAGS=

linux:
    $(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-Wl,-E -ldl -lreadline -lhistory -lncurses"

macosx:
    $(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-lreadline"
# use this on Mac OS X 10.3-
#   $(MAKE) all MYCFLAGS=-DLUA_USE_MACOSX

mingw:
    $(MAKE) "LUA_A=lua51.dll" "LUA_T=lua.exe" \
    "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \
    "MYCFLAGS=-DLUA_BUILD_AS_DLL" "MYLIBS=" "MYLDFLAGS=-s" lua.exe
    $(MAKE) "LUAC_T=luac.exe" luac.exe

posix:
    $(MAKE) all MYCFLAGS=-DLUA_USE_POSIX

solaris:
    $(MAKE) all MYCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" MYLIBS="-ldl"

# list targets that do not create files (but not all makes understand .PHONY)
.PHONY: all $(PLATS) default o a clean depend echo none

# DO NOT DELETE

lapi.o: lapi.c lua.h luaconf.h lapi.h lobject.h llimits.h ldebug.h \
  lstate.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h \
  lundump.h lvm.h
lauxlib.o: lauxlib.c lua.h luaconf.h lauxlib.h
lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h
lcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \
  lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lgc.h \
  ltable.h
ldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h
ldebug.o: ldebug.c lua.h luaconf.h lapi.h lobject.h llimits.h lcode.h \
  llex.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h \
  lfunc.h lstring.h lgc.h ltable.h lvm.h
ldo.o: ldo.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
  lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lparser.h lstring.h \
  ltable.h lundump.h lvm.h
ldump.o: ldump.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h \
  lzio.h lmem.h lundump.h
lfunc.o: lfunc.c lua.h luaconf.h lfunc.h lobject.h llimits.h lgc.h lmem.h \
  lstate.h ltm.h lzio.h
lgc.o: lgc.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
  lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h
linit.o: linit.c lua.h luaconf.h lualib.h lauxlib.h
liolib.o: liolib.c lua.h luaconf.h lauxlib.h lualib.h
llex.o: llex.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h ltm.h \
  lzio.h lmem.h llex.h lparser.h lstring.h lgc.h ltable.h
lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h
lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
  ltm.h lzio.h lmem.h ldo.h
loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h
lobject.o: lobject.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h \
  ltm.h lzio.h lmem.h lstring.h lgc.h lvm.h
lopcodes.o: lopcodes.c lopcodes.h llimits.h lua.h luaconf.h
loslib.o: loslib.c lua.h luaconf.h lauxlib.h lualib.h
lparser.o: lparser.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \
  lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h \
  lfunc.h lstring.h lgc.h ltable.h
lstate.o: lstate.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
  ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h llex.h lstring.h ltable.h
lstring.o: lstring.c lua.h luaconf.h lmem.h llimits.h lobject.h lstate.h \
  ltm.h lzio.h lstring.h lgc.h
lstrlib.o: lstrlib.c lua.h luaconf.h lauxlib.h lualib.h
ltable.o: ltable.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
  ltm.h lzio.h lmem.h ldo.h lgc.h ltable.h
ltablib.o: ltablib.c lua.h luaconf.h lauxlib.h lualib.h
ltm.o: ltm.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h lzio.h \
  lmem.h lstring.h lgc.h ltable.h
lua.o: lua.c lua.h luaconf.h lauxlib.h lualib.h
luac.o: luac.c lua.h luaconf.h lauxlib.h ldo.h lobject.h llimits.h \
  lstate.h ltm.h lzio.h lmem.h lfunc.h lopcodes.h lstring.h lgc.h \
  lundump.h
lundump.o: lundump.c lua.h luaconf.h ldebug.h lstate.h lobject.h \
  llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h lundump.h
lvm.o: lvm.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
  lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h
lzio.o: lzio.c lua.h luaconf.h llimits.h lmem.h lstate.h lobject.h ltm.h \
  lzio.h
print.o: print.c ldebug.h lstate.h lua.h luaconf.h lobject.h llimits.h \
  ltm.h lzio.h lmem.h lopcodes.h lundump.h

# (end of Makefile)