4

I have application that contain statically linked lua 5.2 inteperpreter (and haven't access to code). When I trying to write extension with next code:

#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;
    }
}

And compile it with statically linked lua52.lib . I get "multiple vms detected" error when I trying to require it fromn lua code. What I can do in this situation?

1 Answer 1

6

You can't compile it with statically linked lua52.lib as the main application loads its own version of lua52.lib and when this module is "required", it loads its own copy, which leads to "multiple VMs detected" message.

With statically compiled VM you have two options (on Windows): (1) include all your modules statically, or (2) compile your modules against Lua52.dll, but instead of the actual DLL include a "proxy" DLL that will forward Lua API calls to the methods in the statically compiled executable (the API methods also need to be exported in the executable).

See this thread for the discussion on how the executable needs to be compiled and LuaProxyDllFour page for the proxy DLL.

On Linux you don't need to have a proxy library, but you still need to avoid linking Lua interpreter into the library and export symbols from the executable by using -Wl,-E linker option; see lhf's answer for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.