I wrote the following C file (for lua 5.1) bar_src.c
:
#include <lua5.1/lua.h>
#include <lua5.1/lauxlib.h>
#include <lua5.1/lualib.h>
static int foo(lua_State *L)
{
int a = luaL_checknumber(L, 1);
int b = luaL_checknumber(L, 2);
lua_pushnumber(L, a + b);
return 1;
}
luaL_Reg foolib[] = {
{"foo", foo},
{NULL, NULL}
};
int luaopen_bar(lua_State *L)
{
luaL_register(L, "bar", foolib);
return 1;
}
And compiled it with: gcc -shared -fpic -o bar.so bar.c -llua5.1
.
However, whenever I try running require("bar")
in lua, it instantly does segmentation fault (core dumped)
.
I wanted to try on another system, so I did the same thing on repl.it (which runs on Ubuntu 18.04) and everything worked perfectly, meaning my Linux configuration has an issue.
Edit:
I found the core dump for the segfault in /var/lib/systemd/coredump
, and loaded it into GDB with gdb $(which lua) /var/lib/systemd/coredump/core.lua.[file]
, and gdb said:
Core was generated by `lua'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x00005620820f3ad3 in lua_pushvalue ()