這是一個非常具體的編譯問題,涉及C++,SWIG和Lua。使用共享dll與最小的C++/SWIG/Lua代碼鏈接錯誤
我有一個非常簡單的基本代碼:
[AClass.hpp]
class AClass {
public:
AClass();
};
[AClass.cpp]
#include "AClass.hpp"
AClass::AClass() {}
[的main.cpp ]
#include "AClass.hpp"
int main() {
AClass my_a;
}
在這一點上,沒有與編譯。 我首先編譯libengine.dll中的類,然後使用共享庫構建可執行文件。
讓我們介紹了一大口模塊,並把它添加到DLL:
[AClass.i]
%module M_AClass
%{
#include "AClass.hpp"
%}
%include "AClass.hpp"
今後,在可執行文件鏈接的一切的時候,我得到了以下錯誤:
g++ -c main.cpp
g++ -c AClass.cpp
swig.exe -c++ -lua AClass.i
g++ -Iinclude -c AClass_wrap.cxx
g++ AClass.o AClass_wrap.o -shared -o libengine.dll -Wl,--out-implib,libengine.dll.a -L. -llua5.1
Creating library file: libengine.dll.a
g++ main.o libengine.dll.a -o main.exe
main.o:main.cpp:(.text+0x16): undefined reference to `AClass::AClass()'
collect2: ld returned 1 exit status
有人會有線索嗎?我試圖用nm來調查dll,但我無法想象如何在共享庫中添加另一個.o可以「隱藏」一種方法(這不是特定於構造函數的)。
要重現的背景下,這裏是放在一個目錄下建立測試所需的文件:
include/ # Contains "lauxlib.h", "lua.h" & "luaconf.h"
liblua5.1.dll
AClass.hpp
AClass.cpp
AClass.i
main.cpp
Makefile
最後,這裏是Makefile文件內容:
ifneq (,$(findstring Linux,$(shell uname -o)))
EXEC := main
LIB := libengine.so
LIB_FLAGS := -o $(LIB)
else
EXEC := main.exe
LIB := libengine.dll.a
LIB_FLAGS := -o libengine.dll -Wl,--out-implib,$(LIB)
#NO DIFFERENCE using ".dll.a" as in CMake (option: -Wl,--out-implib,) or only ".dll"
ifdef SystemRoot
# Pure Windows, no Cygwin
RM := del /Q
endif
endif
LANG_LIB := -L. -llua5.1
LANG_INC := include
LANG_SWIG := -lua
all: clean $(EXEC)
clean:
$(RM) main *.exe *_wrap.cxx *.o libengine.*
$(EXEC): main.o $(LIB)
g++ $^ -o [email protected]
main.o: main.cpp
g++ -c $<
#NO PB without dependency to AClass_wrap.o
$(LIB): AClass.o AClass_wrap.o
g++ $^ -shared $(LANG_LIB) $(LIB_FLAGS)
AClass.o: AClass.cpp
g++ -fPIC -c $<
AClass_wrap.o: AClass_wrap.cxx
g++ -fPIC -I$(LANG_INC) -c $<
AClass_wrap.cxx: AClass.i
swig -c++ $(LANG_SWIG) $<
這在Windows 7下進行了測試,使用了MingGW g ++ v4.5.2,SWIG 2.0.2和Lua5.1。
編輯:當SWIG導出到tcl時,問題也出現。但是,在Linux下編譯絕對沒有問題。我比較了生成的AClass_wrap.cxx,它們是相似的。
非常感謝! 我從來沒有找到解決方案,並最終決定忘記共享庫。下次我會試試! –