Skip to content

Compile Lua in VS2019 from sources

OnlineCop edited this page Oct 18, 2021 · 1 revision

Environment

  • I am running Windows 10 Professional.

Note: This creates a single solution containing three distinct projects. These steps differ from this YouTube video, as the video instructs the viewer to change the project 3 different times, generating different output on each compile. The steps below allow you to generate all files from the same solution.

  1. Open Visual Studio.

  2. Under "Create a new project" search for "Blank Solution".

    • It's under "All languages, All platforms, Other" or you can search for it within the templates.
    • I named my solution Lua and saved it in the default C:\Users\kqlives\source\repos directory.
  3. You will create 3 separate projects: one for the library (lua.dll and lua.lib), one for the Lua compiler (luac.exe), and one for the interpreter (lua.exe). You'll do these steps once for each of the projects:

    1. Add an Empty Project for C++.
      • It's under "C++, Windows, Console" or you can search for "Empty Project [C++]".
    2. Name the project.
      • I named the first project library, the second project compiler and the third project interpreter.
      • By default, the generated binaries use the name of the project, but we'll change those in the steps below.
  4. Change the solution's target from "Debug, x86" to "Release, x64".

    • I've noticed that you can't change this until after there is at least one project created.
  5. Change the Project Dependencies so interpreter depends on library.

    • This ensures that lua.lib is available for interpreter to link to.
  6. For the library project:

    1. Add all .c source files to "Source Files", excluding lua.c and luac.c.
    2. Modify the Project's [Configuration Properties]:
      1. [General] [Target Name]: lua.
      2. [General] [Configuration Type]: Dynamic Library (.dll).
      3. [C/C++] [General] [Additional Include Directories]: Lua source directory.
        • Alternately, you can just add all .h source files to "Header Files".
      4. [C/C++] [Preprocessor] [Preprocessor Definitions]: Add LUA_BUILD_AS_DLL.
  7. For the compiler project:

    1. Add all .c source files to "Source Files", excluding lua.c.
    2. Modify the Project's [Configuration Properties]:
      1. [General] [Target Name]: luac.
      2. [General] [Configuration Type]: Application (.exe).
      3. [C/C++] [General] [Additional Include Directories]: Lua source directory.
        • Alternately, you can just add all .h source files to "Header Files".
  8. For the interpreter project:

    1. Add only lua.c to "Source Files".
    2. Modify the Project's [Configuration Properties]:
      1. [General] [Target Name]: lua.
      2. [General] [Configuration Type]: Application (.exe).
      3. [C/C++] [General] [Additional Include Directories]: Lua source directory.
        • Alternately, you can just add all .h source files to "Header Files".
      4. [Linker] [General] [Additional Library Directories]: $(OutDir).
      5. [Linker] [Input] [Additional Dependencies]: lua.lib.
  9. Build the solution.

    • This should give you two library files (lua.dll, lua.lib) and two binaries (luac.exe, lua.exe).
    • Mine were located under Lua\Release.
  10. Create a permanent location somewhere for the above files as well as for the Lua header files extracted earlier.

    • You will need an include directory within this permanent location, where you will copy all the Lua .h files into.
    • The YouTube video from above put the binaries and include directory under C:\Program Files\Lua.
Clone this wiki locally