My environment is Lua-5.4.2 Luasocket-3.0-rc1. When I run lua script directly, it work success. When i run it through c language, it tell me error.
Error Msg is : PANIC: unprotected error in call to Lua API (error running script: error loading module 'socket.core' from file '/usr/local/lib/lua/5.4/socket/core.so': undefined symbol: lua_gettop) Aborted(core dumped)
Does anyone know why?
lua script code is:(test.lua)
#!/usr/local/bin/lua
local socket = require("socket")
print(socket._VERSION)
c code is:(main.c)
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
int main(void)
{
lua_State *L;
L = luaL_newstate();
luaopen_base(L);
luaL_openlibs(L);
printf("lua enter\n");
if (luaL_dofile(L, "test.lua"))
{
luaL_error(L, "error running script: %s", lua_tostring(L, -1));
}
printf("lua exit\n");
while(1) pause();
lua_close(L);
return 0;
}
tl;dr: Pass
-Wl,-Eto GCC.I was able to reproduce your problem with this Dockerfile:
When I run
lua test.luain the resulting Docker container, it works fine. When I rungcc -o test main.c /usr/local/lib/liblua.a -ldl -lm -Wl,-rpath='/usr/local/lib/lua/5.4/socket' && ./test, I get the same panic that you get.The reason that the standalone Lua binary works and yours doesn't is that the former is linked with
-E:ld's documentation for-Esays this about it:Lua uses
dlopento load C modules that yourequire, and said modules need to call Lua functions, which are linked into your binary, so it makes sense that you need this option. And indeed, when I add-Wl,-Eto the GCC command line, then it works fine for me: