Compile and Setup Lua using CMake and Visual Studio 2012 (C++) on Windows 7

So I’ve been wanting to get Lua to work properly with visual studio 2012 for the last couple of days because lua scripting is really neat for a project I’m working on.  Plus I love the idea of not having to rebuild to get a lua script to work since it works instantly.  So I’ve been searching and searching and trial and error of multiple things and only came across one solution which was a Lua Cmake build by LuaDist.org which provided you w/ a proper build that just worked.  Now the lua documentation had a bare minimum lua cmake file you could use, but me being a beginner this didn’t work out too well and I went back to what I know and used what worked.  Which was the best solution for me at least.  More experienced users probably wouldn’t even have this issue.

Download & Compile with CMake

  • First download (clone/zip) the Latest Lua CMake build from this github repository
  • Download CMake Installer  (if you have not, pretty important step) and allow cmake to automatically add the path to windows (if that doesn’t work make sure you add the bin path to your windows system path eg.: C:\Program Files (x86)\CMake 2.8\bin)
  • Extract the contents to a folder then open up the command line inside the directory (Windows Trick: Open the folder + hold shift & right click and open command line in that folder)
  • Run the cmake command: cmake -G “Visual Studio 11″ 
  • This should create a lua.sln in the current directory.  Open that in Visual Studio 2012.

Building with Visual Studio 2012

  • Build ALL_BUILD then INSTALL in debug and release.  Not sure if the debug/release is necessary, but I do it anyways.
  • There should be a folder called debug or release which contains the .lib file.
  • Create a Lua folder to your C:\ path or what ever path you want and inside add a Include and a Lib folder.
  • Copy all the contents of the debug/release folder to the lib folder. (you probably only need the .lib, but it’s nice to know where the .dll file is located)
  • Copy all the contents from the src folder to your new Include folder.
  • Open up the Include folder and find luaconf.h.orig and rename it to luaconf.h or your project will not compile.

Setting up Visual Studio 2012 Project

  • Create a new empty project and add your main source.cpp file, name it what ever you want.  I like to name mine main.cpp
  • Right click on your project from the solution explorer and select properties or from the menu and select Project -> Properties (ALT + F7)
  • Add your include directory to the Configuration Properties -> C/C++ -> General -> Additional Include Directories
  • Add your library directory to the Configuration Properties -> Linker -> General -> Additional Library Directories
  • Add “lua.lib” to the Configuration Properties -> Linker -> Input -> Additional Dependencies

Testing Lua

From my understanding lua_open() doesn’t work with Lua 5.2.2 so in the tutorials you’ll want luaL_newstate() below is example code you should test the compile.  If this doesn’t work review this tutorial and try it again else report any errors within this tutorial so I can fix them.  Much appreciated:

#include <iostream>
#include "lua.hpp"

/* the Lua interpreter */
lua_State* L;

int main ( int argc, char *argv[] )
{
	/* initialize Lua */
	L = luaL_newstate(); 

	/* load Lua base libraries */
	luaL_openlibs(L);

	/* run the script */
	luaL_dofile(L, "test.lua");

	/* cleanup Lua */
	lua_close(L);

	system("PAUSE");
	return 0;
}

Last but not least create a test.lua file in the directory where you’ve built your project and add a simple print(“test”) to the file.  Make sure you copy the lua.dll from the lib folder to this folder as well and run the exe file and it should output what you’ve added to your lua file.

Hope this helps other beginners like myself.

1 Comment

  1. Setup LuaBridge with Visual Studio 2012 (+ SFML 2.1 Example) | Advance IO · July 29, 2013

    [...] recently I’ve been talking a bit about how I want to get lua to work with my C++ games and this would require me to bind C++ variables, functions and classes to the lua language through [...]

Leave a Reply