在mac os x上编译一个动态库,链接lib lua导致" lua:检测到多个Lua VM"

时间:2015-04-03 16:36:04

标签: c++ macos lua makefile

我正在尝试在macosx上创建可用作lua扩展名的动态库。

这是我的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)

我正在使用Luiz Henrique在page上推荐的建议标志。

尽管如此,我仍然看到错误:

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

我错过了某个链接的标志吗?我看到一篇关于“-Wl”和“-E”的帖子,但我没试过就试过了。

编辑:我试图在lua中使用这个共享库,如下所示:

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

1 个答案:

答案 0 :(得分:2)

正如Etan所说,你不需要将Lua解释器与图书馆本身联系起来;从LUA_LIB行中删除-llua。例如,这是我用来在OSX上构建luasocket库的命令:

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}

[根据您的修改进行更新]:您的目标是lsignal.so,但您尝试从.dylib文件加载。您package.cpath需要引用?.so,或者您需要将目标重命名为lsignal.dylib