使用SWIG和MinGW创建的Lua模块会导致解释器在退出时崩溃

时间:2013-12-25 13:40:29

标签: c++ lua cmake mingw swig

我有以下文件:

hello.h

#ifndef HELLO_H
#define HELLO_H

class Hello {
    private:
        int _number;

    public:
        Hello(int number);
        void printHello(void) const;
};

#endif

HELLO.CPP

#include "hello.h"
#include <iostream>

Hello::Hello(int number)
    : _number(number) {
}

void Hello::printHello(void) const {
    std::cout << "Hello, " << this->_number << std::endl;
}

hello.swg

%module hello
%{
    #include "hello.h"
%}
%include "hello.h"

的CMakeLists.txt

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
FIND_PACKAGE(Lua51)
INCLUDE_DIRECTORIES(${LUA_INCLUDE_DIR})
ADD_LIBRARY(
    "hello"
    SHARED
    "hello_wrap.cxx"
    "hello.cpp"
)
TARGET_LINK_LIBRARIES(hello "${LUA_LIBRARIES}")
SET_TARGET_PROPERTIES(hello PROPERTIES PREFIX "")

我使用SWIG生成hello_wrap.cxx,然后通过CMake创建MinGW Makefile,并构建它们。这导致hello.dll。

打开lua解释器,我输入:

hello=require('hello')
inst=hello.Hello(23)
inst:printHello()

正如我所希望的那样打印出“Hello,23”,因此模块加载似乎正常工作。

然而,当我这样做时:

os.exit()

我收到以下错误:

"This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information."

随后lua.exe崩溃。当我这样做时也会发生这种情况:

hello=require('hello')
os.exit()

有没有人对我如何解决这个问题有任何想法?

提前致谢。

编辑:以下是使用问题行运行test.lua时,depends.exe生成的输出,通过lua.exe

***************************| Module Dependency Tree |***************************
*                                                                              *
* Legend: F  Forwarded Module   ?  Missing Module        6  64-bit Module      *
*         D  Delay Load Module  !  Invalid Module                              *
*         *  Dynamic Module     E  Import/Export Mismatch or Load Failure      *
*                               ^  Duplicate Module                            *
*                                                                              *
********************************************************************************

[   ] LUA.EXE
     [   ] KERNEL32.DLL
          [   ] API-MS-WIN-CORE-RTLSUPPORT-L1-1-0.DLL
          [   ] NTDLL.DLL
          [   ] KERNELBASE.DLL
               [ ^ ] NTDLL.DLL
          [   ] API-MS-WIN-CORE-PROCESSTHREADS-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-HEAP-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-MEMORY-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-HANDLE-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-SYNCH-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-FILE-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-IO-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-THREADPOOL-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-LIBRARYLOADER-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-NAMEDPIPE-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-MISC-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-SYSINFO-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-LOCALIZATION-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-PROCESSENVIRONMENT-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-STRING-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-DEBUG-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-ERRORHANDLING-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-FIBERS-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-UTIL-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-PROFILE-L1-1-0.DLL
          [   ] API-MS-WIN-SECURITY-BASE-L1-1-0.DLL
          [F^ ] NTDLL.DLL
     [   ] MSVCRT.DLL
          [ ^ ] KERNELBASE.DLL
          [ ^ ] NTDLL.DLL
          [   ] API-MS-WIN-CORE-CONSOLE-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-DATETIME-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-DEBUG-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-ERRORHANDLING-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-FIBERS-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-FILE-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-HANDLE-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-HEAP-L1-1-0.DLL
          [   ] API-MS-WIN-CORE-INTERLOCKED-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-LOCALIZATION-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-LIBRARYLOADER-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-MEMORY-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-MISC-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-NAMEDPIPE-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-PROCESSENVIRONMENT-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-PROCESSTHREADS-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-PROFILE-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-STRING-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-SYNCH-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-SYSINFO-L1-1-0.DLL
          [ ^ ] API-MS-WIN-CORE-UTIL-L1-1-0.DLL
     [ ^ ] MSVCRT.DLL
     [   ] LUA52.DLL
          [ ^ ] KERNEL32.DLL
               [F^ ] NTDLL.DLL
          [ ^ ] MSVCRT.DLL
          [*  ] HELLO.DLL
               [ ^ ] KERNEL32.DLL
                    [F^ ] NTDLL.DLL
               [ ^ ] MSVCRT.DLL
               [   ] LIBGCC_S_DW2-1.DLL
                    [ ^ ] KERNEL32.DLL
                         [F^ ] NTDLL.DLL
                    [ ^ ] MSVCRT.DLL
                    [*^ ] LIBGCC_S_DW2-1.DLL
               [   ] LIBSTDC++-6.DLL
                    [ ^ ] KERNEL32.DLL
                         [F^ ] NTDLL.DLL
                    [ ^ ] MSVCRT.DLL
                    [ ^ ] MSVCRT.DLL
                    [ ^ ] LIBGCC_S_DW2-1.DLL
                    [*^ ] LIBGCC_S_DW2-1.DLL
               [*^ ] LIBGCC_S_DW2-1.DLL
[*  ] SNXHK.DLL
     [ ^ ] KERNEL32.DLL
          [F^ ] NTDLL.DLL
     [*^ ] KERNEL32.DLL

编辑:添加'cmake -G“MinGW Makefile”'和构建

的输出

cmake -G“MinGW Makefiles”

-- The C compiler identification is GNU 4.8.1
-- The CXX compiler identification is GNU 4.8.1
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
-- Check for working C compiler: C:/MinGW/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Found Lua51: C:/Users/forgottenmaster/Dropbox/lua/lua-5.2.3/src/liblua.a
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/forgottenmaster/Dropbox/lua/modules/c++

的mingw32-使

Scanning dependencies of target hello
[ 50%] Building CXX object CMakeFiles/hello.dir/hello_wrap.cxx.obj
[100%] Building CXX object CMakeFiles/hello.dir/hello.cpp.obj
Linking CXX shared library hello.dll
[100%] Built target hello

1 个答案:

答案 0 :(得分:0)

以下是一些尝试:

  1. 您的cmake有FIND_PACKAGE(Lua51),这可能会导致hello.dll与Lua 5.1链接,而您正在运行Lua 5.2。

  2. 通过visual studio运行lua test.lua。您可以通过几种方式实现这一点,其中一种方法是运行lua test.lua并使脚本暂停,等待输入;在等待输入时,你运行visual studio并附加到lua进程;然后你按下lua控制台中的enter键,这样脚本就会继续;您可能知道故障发生的位置(发布文件/行#)。

  3. 我怀疑这个问题是一个系统DLL,更有可能的是hello.DLL是用不兼容的设置构建的,比如单线程而不是多线程。检查链接到的Lua是否为mingw,使用相同的编译器设置构建。 make中最重要的信息是构建和链接的最终命令。在制作Lua时检查编译和构建命令是什么(可能为make设置详细标记 - 您发布的内容只显示正在运行g ++的状态消息,而不是显示包含路径的命令行参数等等),并找到如何使用cmake。

  4. 原则上你应该能够复制Lua makefile并用hello.lib和co替换列出lua51.lib等的几行。