6

I am trying to build Lua and QtLua with MSVC 2015 x86 and I have encountered an issue with exported/imported symbols.

Here is what I do forbuilding Lua 5.3.2 (source):

cl /MD /O2 /c /DLUA_BUILD_AS_DLL *.c
ren lua.obj lua.o
ren luac.obj luac.o
link /DLL /IMPLIB:lua5.3.2.lib /OUT:lua5.3.2.dll *.obj 
link /OUT:lua.exe lua.o lua5.3.2.lib 
lib /OUT:lua5.3.2-static.lib *.obj
link /OUT:luac.exe luac.o lua5.3.2-static.lib

So far it works and dependancy walker shows that functions are exported without any mangling.

Then I build QtLua using cmake and I'v got about 100 errors like:

error LNK2019: unresolved external symbol _lua_close referenced in function "public: virtual __thiscall QtLua::State::~State(void)" (??1State@QtLua@@UAE@XZ)

So basically my issue is that the DLL exports lua_close and the linker looks for _lua_close.

After a bit of searching the _lua_close format seems legit as Microsoft documentation states that C symbols called with __cdecl are mangled with a '_' prefix.

However I don't get why the DLL exports unmangled names.

Function declaration when compiled in Lua (C files)

__declspec(dllexport) void (lua_close) (lua_State *L);

Function declaration when compiled in QtLua (C++ files)

extern "C" {
  extern void (lua_close) (lua_State *L);
}
Benjamin T
  • 8,120
  • 20
  • 37

1 Answers1

0

Quick answer

MSVC did not found the lua DLL (or lib).

Long answer

Exported C functions called using __cdecl are not mangled, whereas internal C functions called using __cdecl are mangled with a '_' prefix.

Source: http://wyw.dcweb.cn/stdcall.htm

To fix my issue I took all source files from QtLua and created a qmake project from them. And tadaa! It works! (Well, it doesn't because QtLua doesn't seem to be tested on anything else than gcc, but I got rid of this linker problem).

My conclusion is that:

  • MSVC complains about not finding _function and is in fact looking for function (or maybe both).
  • CMake, even if it found the lua library at config time, failed to pass the proper options to the linker at build time.
Benjamin T
  • 8,120
  • 20
  • 37