1

これは、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でクラスをコンパイルし、次に共有ライブラリを使用して実行可能ファイルをビルドします。

SWIG モジュールを導入して、それを 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 $@

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 Seven、MingGW g++ v4.5.2、SWIG 2.0.2、および Lua5.1 でテストされました。

編集: tcl への SWIG エクスポート時にも問題が発生します。ただし、Linux でのコンパイルにはまったく問題はありません。生成された AClass_wrap.cxx を比較しましたが、似ています。

4

1 に答える 1

0

mingw の下の g++ には __declspec(dllimport/export) が必要な場合があります

于 2011-12-12T04:58:27.597 に答える