Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top