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.