1

in this code i load and run test.lua file

int main (){
    L = luaL_newstate();
    luaL_openlibs(L);
    luaL_dofile(L, "test.lua");
    lua_close(L);
    return 0;
}

my test.lua file contents

print ("s1");
r=require 'simple';
print ("s2");

the simple module is installed before

when run ./lua_c ; output is only: s1

but when run lua test.lua; output is

s1
s2

and r in't nil

markgz
  • 6,054
  • 1
  • 19
  • 41
Baba
  • 852
  • 1
  • 17
  • 31

1 Answers1

1

simple is failing to load or parse or execute. To find problem, use luaL_loadfile instead of luaL_dofile and check the return value. If non zero, there was a load error, which you can pop off the Lua stack and print. If no error, do the lua_pcall(L, 0, LUA_MULTRET, 0)) to run the chuck created by loadfile, and again check return code for error, pop off stack and print. It would be somehting like this:

int main ()
{
    L = luaL_newstate();
    luaL_openlibs(L);

    if (luaL_loadfile(L, "test.lua"))
    {
         cout << "Error: " << lua_tostring(L, -1) << endl;
    }
    else if (lua_pcall(L, 0, LUA_MULTRET, 0))
    {
         cout << "Error: " << lua_tostring(L, -1) << endl;
    }
    else
    {
         // call successful
    }

    lua_close(L);
    return 0;
}

Update: now that you know from the error message that simple.so has undefined symbol: lua_gettop, you know that there is a link error. Perhaps simple.so isn't linked to lua51.so, but since it works from lua.exe, which is linked to lua lib, one would it would work from your app, which is surely linked to it too. Another possibility is that lua.exe is statically linked but simple.so is not linked. Verify that simple.so is linked to lua51.so and that the lib it links to can be found such as via LD_LIBRARY_PATH. Verify lua.exe is linked to same .so.

Oliver
  • 27,510
  • 9
  • 72
  • 103
  • `luaL_loadfile` return zero value – Baba Apr 05 '14 at 00:08
  • 1
    Ok, so loads fine; as described, then run the protected call and surely that one will return non-zero – Oliver Apr 05 '14 at 02:07
  • but why `lua` can run `require simple.so` without problem ? ,in my code i can load `simple.lua` but can't load `simple.so` – Baba Apr 05 '14 at 10:36
  • 1
    @ChiChak right now, it's anyone's guess. The error returned by pcall will tell you. – Oliver Apr 05 '14 at 16:08
  • i write `lua_pcall(L, 0, LUA_MULTRET, 0))` after loadfile, and `lua_pcall` return `2` ! – Baba Apr 06 '14 at 08:09
  • 1
    @ChiChak exactly, if it returns 2 it means there was an error. I have extended answer to show how to print it. – Oliver Apr 06 '14 at 14:57
  • i give error : `‘luaL_tostring’ was not declared in this scope` ; after change `luaL_tostring` to `lua_tostring` i see `error: error loading module 'simple' from file './simple.so': ./simple.so: undefined symbol: lua_gettop` – Baba Apr 06 '14 at 20:23
  • thank you, in my program `lua.a` is compile statically ; i question about this error on lua IRC channel; and reply this anwser `cmtptr: link your app with -rdynamic to tell the linker to export lua's symbols so your lua module can find them` ; my problem was solved – Baba Apr 08 '14 at 15:46
  • @Babyy Ah, of course, it's been such a long time since I used static libs that I forgot that possibility. But now you know how to use loadfile and pcall to get diagnostics that help point to source of problem, good work! – Oliver Apr 08 '14 at 16:44