15

I am trying to use Lua on the iPhone. On Mac OS X, in a normal (non-iOS) Cocoa application, I used the following code:

lua_State* l; l = lua_open(); luaL_openlibs(l); luaL_loadstring(l, "print(\"Hi from Lua\");"); lua_pcall(l, 0, 0, 0); 

I downloaded Lua 5.1.4 from lua.org/ftp and compiled it for Mac OS X. In the Xcode project, I used "Add Existing Framework" to add liblua.a and I used "Add Existing Files" to add the include directory.

This works as expected, and prints the string: "Hi from Lua". When I try the same thing in an iOS project, it gives the errors:

"_luaL_newstate", referenced from: _main in main.o more of the same thing... symbol(s) not found collect2: ld returned 1 exit status 

It seems that the .a file is not linked into the iPhone app. Does anybody know how to make this work?

(By the way, I don't really care that Apple might not accept my app if it has Lua in it.)

2
  • 2
    I don't think Apple will reject if you have Lua in it. It's a great idea. Commented Jun 1, 2009 at 22:41
  • It appears that Apple maybe softening their position on embedded interpreters inside iOS applications; see appleoutsider.com/2010/06/10/hello-lua Commented Jun 10, 2010 at 21:32

3 Answers 3

18

You'll need to compile the Lua .a for ARM, not Intel. If the Lua library uses autoconf, you can use my favorite iphone/autoconf builder: build_for_iphoneos. If it's not autoconf, then you can use that script to get an idea of how to attack it. Sometimes you can just build a Static Library Xcode project, dump all the files into it and hit build. If the build is simple enough, it'll do most of the work for you.

I know it doesn't matter for your use, but Lua-based tools are generally shippable on the app store. You just can't download arbitrary code at run time and interpret it.

Sign up to request clarification or add additional context in comments.

4 Comments

it's Lua, not LUA. And it doesnt' use autoconf, btw
Thank you very much, compiling for the ARM worked. All I did was throw the src folder into a Xcode project, create a static library target, and put all the .h and .c files in the Compile Sources folder of the target, and I have my working library.
You may want to make the download link a bit more visible :) Otherwise, thanks!
14

You might want to check out iPhone Wax. It is a lua/iPhone bridge that lets you write native iPhone apps in pure lua! Here is a pretty good tutorial about it.

Comments

4

If you want to write Lua code for iOS, then check out MOAI immediately: http://getmoai.com/

Its an absolutely enjoyable framework for developing games on iOS and Android, as well as Windows and OSX. Not only that, but it provides a pretty good idea of how to properly implement a Lua-VM based hosting environment for scripting in a cross-platform manner: from MOAI, you can learn a lot about this. I've done 4 titles with MOAI so far, and won't be stopping any time soon .. MOAI absolutely kicks ass!

Also check out LOAD81, which is a similar effort albeit with SDL as the target environment: http://github.com/antirez/load81

(I've contributed a little to the LOAD81 project, specifically giving it features of interest/value to the OpenPandora community. MOAI is more commercial, LOAD81 more hobbyist..)

For those trying to learn Lua and the different methods of integrating the Lua VM in a project for multiple platform targets, both MOAI and LOAD81 can provide a lot of great background and clues about the right way to proceed.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.