Reputation: 611
Under Visual Studio (2017) I am trying to script a C++ program with Lua 5.3 but the linker does not find three function names referenced in my C++ source file:
unresolved external symbol _lua_close
unresolved external symbol _lua_createtable
unresolved external symbol _luaL_newstate
I took the C++ source from the Lua website.
I downloaded the Lua 5.3 dynamic library which does not come with an import library so I created the import library with the MSVC tools like so:
dumpbin /exports E:\Documents\Programmation\Lua5.3\lua53.dll
From the output of dumpbin, I copied the 146 names in a new file "mylua53lib.def" and ran lib to generate the .lib file like so:
lib /def:E:\Documents\Programmation\Lua5.3\mylua53lib.def /OUT:E:\Documents\Programmation\Lua5.3\mylua53lib.lib /machine:x86
The three function names that the linker does not find are indeed not appearing in the output of the dumpbin command.
Upvotes: 4
Views: 740
Reputation: 39390
A binary distribution of Lua intented for dynamic linking on Windows should come with two binary files:
Sometimes the library file will come with an .a
extension, which is more common on Linux (as opposed to .lib
on Windows). If it's a Windows build, though, you can simply pass that file as a linker dependency and it will work just fine. This question deals with differences between the two conventions.
As a side note, in order to make it work, if you create a C++ project in Visual Studio, and add a Source.cpp
as it suggests by default, you'll still get unresolved externals. This is due to the fact that while the C sources compile as C++ code just fine, the linker will expect mangled names for the definitions of the C functions used. This can be prevented by either compiling the file as C code, or, preferrably, by telling it that the names from Lua headers should be unmangled in the linked library using extern "C"
:
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <stdio.h>
}
Upvotes: 1