这是一个非常具体的编译问题,涉及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,它们是相似的。
非常感谢! 我从来没有找到解决方案,并最终决定忘记共享库。下次我会试试! –