Lua: dynamic libraries not enabled. How do I enable them?

2.4k Views Asked by At

I'm getting an error in Lua saying

NLua.Exceptions.LuaScriptException: error loading module 'socket.core' from file '.\socket\core.dll': dynamic libraries not enabled; check your Lua installation

the line throwing this error is the 1st line of code in my .lua

local socket = require("socket")
1

There are 1 best solutions below

0
On

Edit: sorry, didn't read it correctly; this patch is for Linux, not Windows...

Unfortunately Lua does not compile a dynamic library (.so file) by default. I've created two patch-files which provide the necessary changes so the Makefile will create a .so file for you:

Makefile

--- Makefile.orig   2017-04-26 18:12:27.442079208 +0200
+++ Makefile    2017-04-26 20:32:34.818881639 +0200
@@ -42,6 +42,9 @@
 TO_BIN= lua luac
 TO_INC= lua.h luaconf.h lualib.h lauxlib.h lua.hpp
 TO_LIB= liblua.a
+TO_LIBX=liblua.so
+TO_LIBXV=liblua.so.$(V)
+TO_LIBXR=liblua.so.$(R)
 TO_MAN= lua.1 luac.1

 # Lua version and release.
@@ -52,7 +55,7 @@
 all:   $(PLAT)

 $(PLATS) clean:
-   cd src && $(MAKE) $@
+   cd src && $(MAKE) $@ V=$(V) R=$(R)

 test:  dummy
    src/lua -v
@@ -62,12 +65,15 @@
    cd src && $(INSTALL_EXEC) $(TO_BIN) $(INSTALL_BIN)
    cd src && $(INSTALL_DATA) $(TO_INC) $(INSTALL_INC)
    cd src && $(INSTALL_DATA) $(TO_LIB) $(INSTALL_LIB)
+   cd src && $(INSTALL_EXEC) $(TO_LIBX) $(INSTALL_LIB)
+   cd $(INSTALL_LIB) && ln -s $(TO_LIBX) $(TO_LIBXV) && ln -s $(TO_LIBX) $(TO_LIBXR)
    cd doc && $(INSTALL_DATA) $(TO_MAN) $(INSTALL_MAN)

 uninstall:
    cd src && cd $(INSTALL_BIN) && $(RM) $(TO_BIN)
    cd src && cd $(INSTALL_INC) && $(RM) $(TO_INC)
    cd src && cd $(INSTALL_LIB) && $(RM) $(TO_LIB)
+   cd src && cd $(INSTALL_LIB) && $(RM) $(TO_LIBX)
    cd doc && cd $(INSTALL_MAN) && $(RM) $(TO_MAN)

 local:
@@ -90,6 +96,7 @@
    @echo "TO_BIN= $(TO_BIN)"
    @echo "TO_INC= $(TO_INC)"
    @echo "TO_LIB= $(TO_LIB)"
+   @echo "TO_LIBX= $(TO_LIBX)"
    @echo "TO_MAN= $(TO_MAN)"
    @echo "INSTALL_TOP= $(INSTALL_TOP)"
    @echo "INSTALL_BIN= $(INSTALL_BIN)"

src/Makefile

--- src/Makefile.orig   2017-04-26 18:39:24.655503489 +0200
+++ src/Makefile    2017-04-26 18:40:59.715893871 +0200
@@ -29,6 +29,7 @@
 PLATS= aix bsd c89 freebsd generic linux macosx mingw posix solaris

 LUA_A= liblua.a
+LUA_SO=    liblua.so
 CORE_O=    lapi.o lcode.o lctype.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
@@ -43,7 +44,7 @@
 LUAC_O=    luac.o

 ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O)
-ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T)
+ALL_T= $(LUA_A) $(LUA_SO) $(LUA_T) $(LUAC_T)
 ALL_A= $(LUA_A)

 # Targets start here.
@@ -59,6 +60,11 @@
    $(AR) $@ $(BASE_O)
    $(RANLIB) $@

+$(LUA_SO): $(CORE_O) $(LIB_O)
+   $(CC) -shared -ldl -Wl,-soname,$(LUA_SO).$(V) -o $@.$(R) $? -lm $(MYLDFLAGS)
+   ln -sf $(LUA_SO).$(R) $(LUA_SO).$(V)
+   ln -sf $(LUA_SO).$(R) $(LUA_SO)
+
 $(LUA_T): $(LUA_O) $(LUA_A)
    $(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS)

I really hope that the Lua-developers will include the compiling of dynamic libraries by default in the code....