我正在嘗試在macosx上創建可用作lua擴展的動態庫。在mac os上編譯一個動態庫x鏈接到lib lua導致「lua:檢測到多個Lua VMs」
這裏是我的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)
我使用他的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)
lua模塊不應該鏈接lua自己。宿主應用程序應該有lua鏈接。 – 2015-04-03 16:37:47
應該刪除哪些標記以在lua腳本中使用它? – ChristianB 2015-04-03 16:43:20
「-llua」標誌鏈接到lua庫。 – 2015-04-03 16:45:36