Building Lua (on Windows, with Visual Studio)

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).

Building Lua 5.4.7 on Windows 10 (with Visual Studio 2022)

  1. Visual Studio is a prerequisite.

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

  3. Extract the content of the archive file into a directory; I use C:\Lua\lua-5.4.7 in this example.

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

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

    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, build and bin; but that’s just my preference:

    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                                  -> see note below
    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          -> e.g. *-x86.lib or *-x64.lib
    C:\Lua\build> move *.exe ..\bin
    C:\Lua\build> move *.dll ..\bin
    
    Note: Renaming these two compiled object files before linking is necessary to prevent linking errors
    … like this: luac.obj : error LNK2005: _main already defined in lua.obj.
    … or this: lua.dll : fatal error LNK1169: one or more multiply defined symbols found.
  6. There should be three files in the “bin” directory now:

    • lua.exe interprets/runs Lua scripts and provides a REPL (a “shell”).
    • luac.exe converts Lua source code files into binary files that can be loaded and executed by Lua.
      (Such files aren’t standalone executables in the traditional sense; i.e. they can not be run like Windows *.exe files!)
    • lua.dll is a shared library that contains necessary stuff for the two above mentioned executables (lua.exe and luac.exe)

    These three files can be moved anywhere, but they must stick together in one spot:
    All three files must reside in the same directory in order for them to function correctly!
    (I prefer to put them in something like “C:\Program Files\Lua”, which I then also add to my %PATH%, so that I can call lua from anywhere on a command line.)

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

    • Move the “src” subdirectory (with the *.h files) beneath the above mentioned folder (where you put lua.exe, luac.exe and lua.dll; e.g. C:\Program Files\Lua).
    • Move the generated static libraries (i.e. lua-static-[x86|x64].lib) from “build” into the same folder, at the same level as lua.exe and the others.

    Then a build system can be pointed to these locations.