views:

6429

answers:

3
+6  Q: 

Lua on iPhone?

I am trying to use Lua on the iphone. On Mac OSX, in a normal Cocoa application (not iPhone), 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 I compiled it for Mac OSX. In the Xcode project, I have used "add existing framework" to add liblua.a and I have 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 a iPhone 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 do not really care that Apple might not accept my app if it has Lua in it.

+11  A: 

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.

Rob Napier
it's Lua, not LUA. And it doesnt' use autoconf, btw
Javier
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.
nazgul42
A: 

This isn't an answer... but I followed what you said that you did, nazgul42, and it seemed to work(make a Static Library project, then, a static library target was already there*duh* and it seemed to compile properly). It spat out a .a file which i believe is iPhone/xCodes version of the .lib file? I included it in another blank project, and I'm not sure how to include the lua functions? I tried include "lua.h" and stuff, but it doesn't seem to work... how do you include the main lua.h header file?

btw: I put the .a file in my Other Sources folder/group of my project.

Bill
What you have to do is add all the .h files that you compiled earlier to the project you are trying to use Lua from. If you want to use Lua in a project, you must have the .a file, lua.h, lauxlib.h,lua.hpp, luaconf.h, and lualib.h added to that project. I am not sure if this is all correct, but it is what worked for me.
nazgul42
Also, it shouldn't matter what folder you put the .a file in.
nazgul42
I tried what you said and it compiles and runs fine... my main function looks like this:- (void)viewDidLoad { lua_State *L = lua_open(); lua_close(L); [super viewDidLoad];}but when I add lua_dofile(L, "script.lua"); problems happen,1: No highlighting happens to lua_dofile... which means its not there.2: I tried to compile even knowing that, and it says "implicit declaration of lua_dofile".please help?
Bill
Till
+4  A: 

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.

probablyCorey