简体   繁体   中英

Compiling a dynamic library on mac os x linking against lib lua results in “lua: multiple Lua VMs detected”

I am attempting to create dynamic library on macosx that can be used as a lua extension.

Here is my makefile:

SOURCES= src/lsignal.cpp
OBJ= lsignal.o
CC= clang++
LUA_PATH= third_party/lua-5.2.3/src
LUA_INCLUDE= -I include -I $(LUA_PATH) 
LUA_LIB= -L $(LUA_PATH) -llua
# linux
CFLAGS_LINUX= -fPIC
# macosx
CFLAGS_MACOSX=
# linux
LFLAGS_LINUX= -shared 
# macosx
LFLAGS_MACOSX= -bundle -undefined dynamic_lookup
# target
TARGET= lsignal.so
# placeholder
CFLAGS= -Wall -I include -I $(LUA_PATH) -c
LFLAGS= $(LUA_LIB) 

# ho-lee-sheet they have if statements and os checks
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin) 
    CFLAGS+=$(CFLAGS_MACOSX)
    LFLAGS+=$(LFLAGS_MACOSX)
else
    CFLAGS+=$(CFLAGS_LINUX)
    LFLAGS+=$(LFLAGS_LINUX)
endif

.PHONY: debug
debug: CFLAGS+=-DDEBUG
debug: all 

.PHONY: release
release: CFLAGS+=-O2
release: all

all: lsignal
    @echo "LFLAGS: $(LFLAGS)"
    $(CC) $(LFLAGS) -o $(TARGET) $(OBJ) 
lsignal:
    @echo "CFLAGS: $(CFLAGS)"
    $(CC) $(CFLAGS) $(SOURCES)

clean:
    rm -f $(OBJ) $(TARGET)

I am using the suggested flags recommended by Luiz Henrique on his page .

Despite this I am still seeing the error:

lua: multiple Lua VMs detected
stack traceback:
    [C]: in ?
    [C]: in function 'require'
    test.lua:4: in main chunk
    [C]: in ?
lua(52879,0x7fff746ad300) malloc: *** error for object 0x10aa02938: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
zsh: abort      lua test.lua

Am I missing a flag for linking somewhere? I saw a post about "-Wl" and "-E" but I have tried both without luck.

EDIT: I am attempting to use this shared library in lua like this:

#!/usr/bin/env lua
-- for osx
package.cpath = package.cpath .. ";?.dylib"
local signal = require "lsignal"
print(signal.SIGABRT)

As Etan said, you don't need to link the Lua interpreter with the library itself; remove -llua from your LUA_LIB line. For example, this is the command I use to build luasocket library on OSX:

BUILD_FLAGS="-O2 -dynamiclib -undefined dynamic_lookup -I $INSTALL_DIR/include -L $INSTALL_DIR/lib"
gcc $BUILD_FLAGS -o "$INSTALL_DIR/lib/lua/$LUAV/socket/core.dylib" \
src/{auxiliar.c,buffer.c,except.c,inet.c,io.c,luasocket.c,options.c,select.c,tcp.c,timeout.c,udp.c,usocket.c}

[Update based on your edit]: your target is lsignal.so , but you are trying to load from .dylib file. You package.cpath needs to reference ?.so or you need to rename the target to lsignal.dylib .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM