开发者

Linkage error using shared dll with minimal C++/SWIG/Lua code

This is a really specific compilation problem involving C++, SWIG and Lua.

I have a really simple base code :

[AClass.hpp]

class AClass {
public:
    AClass();
};

[AClass.cpp]

#include "AClass.hpp"

AClass::AClass() {}

[main.cpp]

#include "AClass.hpp"

int main() {
    AClass my_a;
}

At this point, there is no matter with compilation. I first compile the开发者_StackOverflow中文版 class in libengine.dll and then use the shared library to build the executable.

Let's introduce a SWIG module, and add it to the dll :

[AClass.i]

%module M_AClass

%{
#include "AClass.hpp"
%}

%include "AClass.hpp"

Henceforth, when linking everything in an executable, I got the following error :

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

Would anyone have a clue ? I tried looking into the dll with nm but I can't figure how adding another .o to the shared library can "hide" a method (this isn't specific to constructors).


To reproduce the context, here are the necessary files to put in a directory to build the test :

include/ # Contains "lauxlib.h", "lua.h" & "luaconf.h"
liblua5.1.dll
AClass.hpp
AClass.cpp
AClass.i
main.cpp
Makefile

And finally, here is the Makefile content :

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 $@

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) $<

This was tested under Windows Seven, with MingGW g++ v4.5.2, SWIG 2.0.2 and Lua5.1.

EDIT: The problem also appear when SWIG-exporting to tcl. However, there is absolutely no problem compiling under Linux. I compared the generated AClass_wrap.cxx, they are similar.


g++ under mingw might require __declspec(dllimport/export)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜