【问题标题】:Lua - "multiple vms detected" while trying to add extension for statically linked LuaLua - 尝试为静态链接的 Lua 添加扩展时“检测到多个虚拟机”
【发布时间】:2015-07-27 08:23:42
【问题描述】:

我的应用程序包含静态链接的 lua 5.2 解释器(并且无法访问代码)。 当我尝试使用下一个代码编写扩展时:

#define LUA_LIB
#define LUA_BUILD_AS_DLL 
#include "lua.hpp"

extern "C" 
{
    static int test(lua_State* state)
    {
        return 1;
    }

    static const struct luaL_Reg functions[] = {
        {"test", test},
        {NULL, NULL},
    };

    int __declspec(dllexport) luaopen_test(lua_State* state)
    {
        luaL_newlibtable(state, functions);
        luaL_setfuncs(state, functions, 0);
        return 0;
    }
}

并使用静态链接的 lua52.lib 编译它。 当我尝试从 lua 代码中要求它时,出现“检测到多个 vms”错误。 在这种情况下我能做什么?

【问题讨论】:

    标签: lua


    【解决方案1】:

    你不能用静态链接的 lua52.lib 编译它,因为主应用程序加载它自己的 lua52.lib 版本,当这个模块是“必需的”时,它会加载它自己的副本,这会导致“检测到多个 VM”消息。

    使用静态编译的 VM,您有两个选项(在 Windows 上):(1) 静态包含所有模块,或 (2) 针对 Lua52.dll 编译模块,但不是包含实际的 DLL,而是包含一个“代理”DLL会将 Lua API 调用转发到静态编译的可执行文件中的方法(API 方法也需要在可执行文件中导出)。

    请参阅this thread for the discussion 了解需要如何编译可执行文件,并参阅LuaProxyDllFour 代理 DLL 页面。

    在 Linux 上,您不需要代理库,但您仍然需要避免将 Lua 解释器链接到库中并使用 -Wl,-E 链接器选项从可执行文件中导出符号; see lhf's answer for details.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 2012-03-03
      相关资源
      最近更新 更多