0

I am using both VS Code and Visual Studio 2019, on Windows 10. C++ and Lua alone work fine. Everything is updated to newest version.

I have installed Lua in: C:\Program Files\Lua. Under C:\Program Files\Lua\include there are all the files .h, .c and .hpp. For testing purposes, I copied content of folder 'include' to my program's folder, into 'Lua'. So there's [exe directory]/Lua.

This is sample code I got from the internet:

// main.cpp
#include <iostream>
#include "lua/lua.hpp"

using namespace std;

int main(int argc, char* argv[])
{
    lua_State* L = luaL_newstate();
    luaL_dostring(L, "x = 42");
    lua_getglobal(L, "x");
    lua_Number x = lua_tonumber(L, 1);
    cout << "lua says x = " << (int)x << endl;
    lua_close(L); 
}

When I try to run it in VS Code (alt + ctrl + N, combination from addon), I get:

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Sokrates\AppData\Local\Temp\ccVATT2h.o:mainLauncher.c:(.text+0x15): undefined reference to `luaL_newstate'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Sokrates\AppData\Local\Temp\ccVATT2h.o:mainLauncher.c:(.text+0x2c): undefined reference to `luaL_loadstring'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Sokrates\AppData\Local\Temp\ccVATT2h.o:mainLauncher.c:(.text+0x5f): undefined reference to `lua_pcallk'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Sokrates\AppData\Local\Temp\ccVATT2h.o:mainLauncher.c:(.text+0x6d): undefined reference to `lua_close'
collect2.exe: error: ld returned 1 exit status

I found few different 'fixes' but they all were for Linux e.g. this or this. So not really helpful for Win10.

In VS 2019 I added all these .h and .c files to 'Source Files' (except for luac.c and lua.c). Code didn't work without any '#include', so I wrote #include "lua.hpp". I got an error "cannot open source file "lua.hpp"". Then I tried copying the whole folder with .h and .c files into program's directory and use #include "Lua/lua.hpp". I got back to the same errors as with VS Code.

Some other questions and answers under them mentioned the parameter "-llua". I tried it in manual build: "g++ main.cpp -o main -llua", however I got:

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -llua
collect2.exe: error: ld returned 1 exit status

Last minute observation: Setting VS 2019 solution to "debug x64" seems to have fixed the problem there (code ran and displayed number correctly ; still needed copy of all .h & .c files in the exe directory). But I don't know how to apply it to VS Code (I'd prefer to use VS Code over VS 2019). I found this but neither clickable option nor "Switch to Windows PowerShell (x64)" appear there, even after installing the extension.

Eight
  • 1
  • 2
  • For your mingw build which you need to use msys2 to install lua. Related: [https://packages.msys2.org/search?q=lua](https://packages.msys2.org/search?q=lua) – drescherjm Feb 24 '22 at 20:00
  • for gcc you do need `-llua` but you also need `-L` with the directory containing the lua libraries – Alan Birtles Feb 24 '22 at 20:22
  • ***I have installed Lua in: C:\Program Files\Lua. Under C:\Program Files\Lua\include there are all the files .h, .c and .hpp. For testing purposes, I copied content of folder 'include' to my program's folder, into 'Lua'. So there's [exe directory]/Lua.*** I would not use this method. lua exists in the msys2 package manager. It's best to have msys2 manage your libraries. If you do that it will install them in the system location (where your compiler and linker already look) and you will not need to use -I or -L to specify different folders. – drescherjm Feb 24 '22 at 20:22
  • @drescherjm Thanks for the link and explanation, however it didn't work. I tried installing few different versions, but even after restart, the error kept repeating. Also - I know my method wasn't too good - but I had to try everything and it was most promising. – Eight Feb 24 '22 at 21:00
  • @AlanBirtles Thank you, that actually solves all problems. Well, I can't really use shortcut now, but oh well - at least it works! – Eight Feb 24 '22 at 21:00
  • You probably should have followed it to: [https://packages.msys2.org/package/mingw-w64-x86_64-lua?repo=mingw64](https://packages.msys2.org/package/mingw-w64-x86_64-lua?repo=mingw64) and typed `pacman -S mingw-w64-x86_64-lua` in the mingw64 terminal to install the 64 bit version. – drescherjm Feb 24 '22 at 21:07
  • @drescherjm That is literally what I did haha that version, that command and that specific terminal. Restart aaand... no change. – Eight Feb 24 '22 at 21:26
  • Odd. I do notice that your mingw is not current. The current version is 11.2 – drescherjm Feb 24 '22 at 21:57
  • @drescherjm Indeed. I've just updated everything, what was updatable. Generally a good point, however in this case it doesn't change anything - error is still there and looks the same (but with newer version numbers). – Eight Feb 24 '22 at 22:56

0 Answers0