Search code examples
c#c++linkerlualuainterface

Building Lua Modules for LuaInterface


I have a Lua module (lpack) that I need to load into LuaInterface. I've tried a few different configurations for the project, but I can't seem to figure out the linking.

I get the error unresolved external symbol "void __stdcall lua_replace(struct lua_State *,int)" (?lua_replace@@YGXPAUlua_State@@H@Z) referenced in function "int __stdcall l_unpack(struct lua_State *)" (?l_unpack@@YGHPAUlua_State@@@Z)

If I run dumpbin /exports on the lua51.lib file, I can find ?lua_replace@@YAXPAUlua_State@@H@Z which matches what I find in the lua51.dll with Dependency Walker ?lua_replace@@YAXPAUlua_State@@H@Z, so the function exists but the names don't match.

I'm not very familiar with this level of linker management, so this may be a simple error on my part. Why is the value in the lpack build not matching that of the lua51.lib input file?

Update

All of the exports in the lua51.dll and lua51.lib files were __cdecl and my lpack project has been set to __stdcall. Changing that property (under C/C++ -> Advanced -> Calling Convention) fixed the compile issue and I now have an lpack.dll file.

However, when I attempt to load the file from lua require('lpack') it fails with The specified procedure could not be found.

Breaking in the lua C code that loads the library with LoadLibraryA returns NULL and a GetLastError() code of 0x7F.

Update 2

The lpack.dll file I was attempting to load was out of date due to a wrong output directory, so that fixed the LoadLibrary failure. Next the GetProcAddress would fail, but if I added extern "C" to the export function in the lpack.c file as per Mud's recommendation, the problem was resolved.


Solution

  • However, when I attempt to load the file from lua require('lpack') it fails with The specified procedure could not be found.

    Probably because the name is mangled. Did you look in the DLL to see the actual (mangled) name of the function?

    You really ought to build the Lua source (and any Lua modules written in C) as C, and use extern "C" when importing them. Save you a lot of headaches.