Building Lua 5.4.7 on Windows 10 with Visual Studio 2022

Although one can get pre-built binaries of the Lua (programming language) interpreter, to me that seems to be just a semi-offical or lackluster way.
But luckily, building it yourself from source is very easy (especially with the help of this post from Dennis D. Spreen).

  1. Prerequisite: Visual Studio is already set up.

  2. Download the Lua source code archive file from Lua.org → Download.

  3. Extract the content of the archive file (e.g. with 7-Zip) into a directory (I use C:\Lua\lua-5.4.7 in this example).

  4. Start the Visual Studio Developer Command Prompt from the start menu:

    • For 32-bit results: “x86 Native Tools Command Prompt for VS 2022”
    • For 64-bit results: “x64 Native Tools Command Prompt for VS 2022”
  5. Do the following and adjust the paths, filenames (x86 or x64 prefix), etc. as needed:

    Note A: I like to keep the original stuff, the built artifacts and the generated binaries separately; that’s why the directory structure looks as it does here, with two additional subdirectories: built and bin. But that’s of course just my preference, you can handle it differently – but then you also may need to adept the steps!.

    C:\>          cd C:\Lua
    C:\Lua\>      md build
    C:\Lua\>      md bin
    C:\Lua\>      cd build
    C:\Lua\build> cl /MD /O2 /c /DLUA_BUILD_AS_DLL ..\lua-5.4.7\src\*.c
    C:\Lua\build> rename lua.obj lua.o
    C:\Lua\build> rename luac.obj luac.o
    C:\Lua\build> link /DLL /IMPLIB:lua.lib /OUT:lua.dll *.obj
    C:\Lua\build> link /OUT:lua.exe lua.o lua.lib
    C:\Lua\build> lib /OUT:lua-static.lib *.obj
    C:\Lua\build> link /OUT:luac.exe luac.o lua-static-x86.lib
    C:\Lua\build> move *.exe ..\bin
    C:\Lua\build> move *.dll ..\bin
    

    Note B: Renaming two of the compiled object files before linking is necessary to prevent such linking errors:

    luac.obj : error LNK2005: _main already defined in lua.obj.
    ...
    lua.dll : fatal error LNK1169: one or more multiply defined symbols found.
    
  6. At the end, there should be now three files in the bin directory:

    • lua.exe interprets/runs scripts and provides a REPL (a “shell”).
    • luac.exe converts Lua code into binary files that can be loaded and executed by Lua.
      Note: These are then not standalone executables in the traditional sense; i.e. they are NOT common Windows *.exe files!
    • lua.dll is the library that contains further necessary stuff for those two executables and all three must be kept in the same directory in order for them to function correctly.

    These three files can be moved elsewhere, but must stay together! I prefer to put them in something like C:\Program Files\Lua (which I then also add to my %PATH%, so that I could call lua from anywhere on the command line).

  7. For using Lua with C or C++, the header files and libraries must be accessible by the compiler/build system. I therefore usually do this at the end:

    • The whole Lua src subdirectory (with the *.h files) is moved by me into the above mentioned folder, C:\Program Files\Lua.
    • The static libraries from build (i.e. lua-static-[x86|x64].lib) are also moved by me into that top-level folder (next to lua.exe etc.).

    Then a build system can be pointed to the required items there.