gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:57:35 24 4
gpt4 key购买 nike

我有以下文件:

你好.h

#ifndef HELLO_H
#define HELLO_H

class Hello {
private:
int _number;

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

#endif

你好.cpp

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

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

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

你好.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()

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

提前致谢。

编辑:以下是 depends.exe 通过 lua.exe 运行带有问题行的 test.lua 时产生的输出

***************************| 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
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* ] SNXHK.DLL
[ ^ ] KERNEL32.DLL
[F^ ] NTDLL.DLL
[*^ ] KERNEL32.DLL

编辑:添加了 'cmake -G "MinGW Makefiles"' 和构建的输出

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-make

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. 您的 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 并将列出 lua51.lib 等的几行替换为 hello.lib 和 co。

关于c++ - 使用 SWIG 和 MinGW 创建的 Lua 模块,导致解释器在退出时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20773386/

24 4 0
文章推荐: javascript - 如何在调整浏览器窗口大小时使固定宽度的div插入另一个具有相对宽度的div?
文章推荐: java - LAN 网络中的连接系统-Java
文章推荐: html - 忽略来自另一个
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com