我在Windows上使用Lua5.1编译了一个Lua扩展dll,在Lua命令行模式下运行良好,但无法在OpenResty中使用

lua-pack.c是一种名为lua-pack.c的c代码,提供此处,用于处理十六进制字符串的Lua扩展,我需要在OpenResty中使用它来处理一些网络流。

我在Windows中使用Lua5.1和Luarocks安装了它

我成功得到了lua-pack.dll. 所有的测试在命令行模式下都可以成功运行,就像这样

lua C:\projects\lua-pack\lua-pack\test.lua

但一旦我将这个lua-pack.dll放在openresty的目录中并使用它时,一些异常就会被抛出,如

worker process 24892 exited with code C0000005

我能够在Ubuntu上成功编译lua-pack.so(使用makefile编译),并且它在Linux中在OpenResty中运行正常。我尝试将这个lua-pack.so复制到Windows中,但抛出异常说这个so不适用于Windows使用。

然而,我注意到在作者提供的test.lua此处中,它要求(require(pack))而不是(require(lua_pack))

所以我将lua-pack.dll重命名为pack.dll,然后我得到了另一个异常

lua entry thread aborted: runtime error: error loading module 'pack' from file '.\pack.dll':

我确实注意到这是Windows中的pack.lua Rocks,我不知道这是否重要。

作者提供的test.lua的开始如下,可以在Windows命令行中运行

require"pack"
bpack=string.pack
bunpack=string.unpack

我的代码在Linux Openresty中的启动方式如下

local bpack, bunpack
do
    local string_pack = string.pack
    local string_unpack = string.unpack
    require "lua_pack"
    bpack = string.pack
    bunpack = string.unpack
    -- luacheck: globals string.unpack
    string.unpack = string_unpack
    -- luacheck: globals string.pack
    string.pack = string_pack
end
点赞