Lua on the iPhone
27/Jul/2010 10:59 PM Filed in: Programming
I was first introduced to Lua 3 years ago just after I
started grad school; I first read about it in
Mat Buckland’s “Programming Game AI by Example,”
a reference I still always keep in arms reach.
As a total n00b in the universe of game
development back then, learning how Lua could be
integrated into my game code without really
having more critical fundamentals down first
shelved any use I had for the language pretty
quick. Well, things are different now, and as a
professional game developer, it’s time I give
Lua the respect it deserves.
However, in an unexpected twist of fate, since the time I began to devote myself to the art of game development, I’ve left C++ and become a staunch Objective-C devout. And while the circumstances and justifications for this progression I leave to a future post, it’s a choice that requires I be prepared to integrate some external libraries, such as Lua, within Apple’s development toolchain so that I can use them on the iPhone.
Thankfully, Lua’s developer’s strict adherence to ANSI C makes getting Lua on the iPhone a total breeze!
Start by downloading the 5.1.4 stable release of Lua: http://www.lua.org/ftp/
Open up Xcode (I was using the latest 3.2.3 64-bit release), and create a new iPhone Static Library. If you haven’t already, uncompress the source code, and drag & drop the “src” folder into your Xcode project.
Note: Decide if you want to check the box that says “Copy items into destination group’s folder (if necessary).” I left mine unchecked, but you may decide it is something you need.
Here you can see what throwing the “src” folder into your project looks likes:
Compile. You may already know this, but since we’re compiling a static library for use with the iPhone, you need to recompile the project for both the simulator and the device depending on where you’re deploying.
Create a new project, I recommend Window-based Application.
Add your static library, (“Add Existing Framework > Add Other...”)
Include Lua’s “src” folder path in the “User Header Search Path” within your project’s settings.
Modify your delegate’s implementation file to include the following headers:
Add these lines to your delegate’s didFinishLaunching or didFinishLaunchingWithOptions: method:
lua_State* l;
l = lua_open();
luaL_openlibs(l);
luaL_loadstring(l, "print(\"Hello, Lua!\");");
lua_pcall(l, 0, 0, 0);
You should end up with this result:
Enjoy Lua from within your iPhone apps!
However, in an unexpected twist of fate, since the time I began to devote myself to the art of game development, I’ve left C++ and become a staunch Objective-C devout. And while the circumstances and justifications for this progression I leave to a future post, it’s a choice that requires I be prepared to integrate some external libraries, such as Lua, within Apple’s development toolchain so that I can use them on the iPhone.
Thankfully, Lua’s developer’s strict adherence to ANSI C makes getting Lua on the iPhone a total breeze!
Start by downloading the 5.1.4 stable release of Lua: http://www.lua.org/ftp/
Open up Xcode (I was using the latest 3.2.3 64-bit release), and create a new iPhone Static Library. If you haven’t already, uncompress the source code, and drag & drop the “src” folder into your Xcode project.
Note: Decide if you want to check the box that says “Copy items into destination group’s folder (if necessary).” I left mine unchecked, but you may decide it is something you need.
Here you can see what throwing the “src” folder into your project looks likes:

Compile. You may already know this, but since we’re compiling a static library for use with the iPhone, you need to recompile the project for both the simulator and the device depending on where you’re deploying.
Create a new project, I recommend Window-based Application.
Add your static library, (“Add Existing Framework > Add Other...”)
Include Lua’s “src” folder path in the “User Header Search Path” within your project’s settings.
Modify your delegate’s implementation file to include the following headers:
- #import “lua.h”
- #import “lauxlib.h”
- #import “lualib.h”
Add these lines to your delegate’s didFinishLaunching or didFinishLaunchingWithOptions: method:
lua_State* l;
l = lua_open();
luaL_openlibs(l);
luaL_loadstring(l, "print(\"Hello, Lua!\");");
lua_pcall(l, 0, 0, 0);
You should end up with this result:

Enjoy Lua from within your iPhone apps!