4

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.

Community
  • 1
  • 1
Itsbananas
  • 569
  • 1
  • 4
  • 12
  • I don't quite get what you're trying to do there; normally, you'd just have to `extern "C" { #include }` and it should work. – DarkWiiPlayer Jun 27 '19 at 11:02
  • @DarkWiiPlayer Well as I said I took the example from the Lua website so I included the three headers. However I indeed put the three include directives in a extern "C" { ... }. – Itsbananas Jun 27 '19 at 11:05
  • *which does not come with an import library* - wait, what? This doesn't sound correct. I don't remember ever having to jump through such hoops. If you download the binaries from e.g. [here](http://luabinaries.sourceforge.net/), it comes with headers, DLL and the `.a` file (which is effectively the lib file). – Bartek Banachewicz Jun 27 '19 at 11:49
  • @BartekBanachewicz So changing the .a extension to .lib is enough? Ok, thanks! – Itsbananas Jun 27 '19 at 12:07

1 Answers1

1

A binary distribution of Lua intented for dynamic linking on Windows should come with two binary files:

  • a DLL file with the actual Lua code
  • a library file with the method stubs delegating to the DLL

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>

}
Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
  • Of course, the next preferred step would be to replace `stdlib.h` and `stdio.h` with either `cstdlib` and `cstdio`, or even better, to replace those with proper C++ code, if the project is intended to be written in C++ and not C. – Bartek Banachewicz Jun 27 '19 at 12:27