Skip to main content
gamedev.net
🔒 Locked

[C++] Lua integration

Started by Terradigits 17 replies 11.8k views
Original Post
Terradigits
Terradigits
Hey everyone, I was interested in trying to implement a lua interpreter into my app, but I can't seem to get things working. I was actually following a tutorial here on gamedev (I don't have a link on me at the moment), but it was a bit outdated; I was unable to get lua to compile. Are there any tutorials floating around on the internet I could use? I couldn't really find much of anything.
Zerd
Zerd
On the official Lua website you can find a lot of information.

Here is a description how you can build lua with several different compilers
Terradigits
Terradigits
I checked out the links, but they werent really what I was looking for. I'll just ask a specific question. I was trying to follow this tutorial: http://www.gamedev.net/reference/articles/article1932.asp

My problem is that it says that 'lua.h' doesnt exist, and I have no idea why. I have my sourcecode in the right spot and everything. i'm using Dev C++
rip-off
rip-off
Quote:
Original post by Terradigits
I checked out the links, but they werent really what I was looking for. I'll just ask a specific question. I was trying to follow this tutorial: http://www.gamedev.net/reference/articles/article1932.asp

My problem is that it says that 'lua.h' doesnt exist, and I have no idea why. I have my sourcecode in the right spot and everything. i'm using Dev C++


Where is lua.h in your dev-cpp/include folder? If its in a sub-directory like dev-cpp/include/lua/lua.h (like mine) you will need to add this directory to your build path.

If lua is a library you use or will use often, add it to the compilers build path in tools->compilers->directories->(C/C++) includes

If its only used in this project and wont be using often, you can add it just to that project in project->project options->directories->includes

Finally, if you are using lua in C++ you should #include "lua.hpp" and not lua.h.
Joakim_ar
Joakim_ar
If you're simply trying to compile the LUA source, then you're in luck since I've just done exactly that.

First add the mingw bin path (came with dev-cpp) to your PATH environment variable. This is done in the control panel under System->Advanced->Environment variables. Under 'system variables' find the one called 'Path' and add a semicolon and the dev-cpp bin path to the end so it looks something like this:
%SystemRoot%\system32[...];C:\Dev-Cpp\bin

Now you can use a lot of wonderful compilation (et al) tools, including make which is what you need here. Press Start->Run and type in 'cmd' [enter]. Then cd to your lua dir, ie. 'cd c:\lua' and write 'make mingw' [enter].

That's it. Happy programming!
Terradigits
Terradigits
Well, I have the lua.h problem fixed, but a new one has arisen. The following code seems to no longer work:

#include <stdio.h>extern "C"{ #include "lua.h"} int main(int argc, char* argv[ ]){   lua_State* luaVM = lua_open(0);    if (NULL == luaVM)   {      printf("Error Initializing lua\n");      return -1;   }    // Do stuff with lua code.       lua_close( luaVM );    return 0;}


it states that lua_open is undeclared.
Zorbfish
Zorbfish
That's because the article was written for version 4.0.1. I assume you are using at least 5.0 or the newest release 5.1.1. Beyond version 4 lua_open was redefined to not take a stack size argument.

Try:
extern "C"{  #include "lua.h"}int main(int argc, char *argv[]){  lua_State *L = lua_open();  if ( !L ) {    printf("Error initializing Lua.\n");    return -1;  }  // ...  lua_close(L);  return 0;}
RDragon1
RDragon1
If you're binding C++ and LUA I highly suggest using luabind:

http://www.rasterbar.com/products/luabind/docs.html
EmrldDrgn
EmrldDrgn
Quote:
Original post by Terradigits
Well, I have the lua.h problem fixed, but a new one has arisen. The following code seems to no longer work:

*...snip...*

it states that lua_open is undeclared.


I had the identical problem awhile back. Lua 5.0 doesn't use lua_open (at least not directly... it should still work, technically, but it doesn't). Check out some documentation on luaL_newstate() on the Lua site. Basically, you can look at this as the "new" lua open. Actually, I find Lua 4.0 to be much easier than 5.* on the C/++ side of things (though that's just my opinion), so you might consider just using 4 for learning purposes. Certainly it'll make that tutorial easier to follow.

Another note: lua.hpp > lua.h for C++. It's basically this:
extern "C"{#include lua.h#include lauxlib.h#include // some other header... not sure which}

which makes it infinitely better for C++, as it includes EVERYTHING you need.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~I program in C++, on MSVC++ '05 Express, and on Windows. Most of my programs are also for windows.
Zorbfish
Zorbfish
Quote:
Original post by EmrldDrgn
Actually, I find Lua 4.0 to be much easier than 5.* on the C/++ side of things (I can't seem to find a simple "DoFile" method in 5.*), so you might consider just using 4 for learning purposes. Certainly it'll make that tutorial easier to follow.


In 5.0:
lua_dofile in lauxlib.h

In 5.1:
luaL_dofile in lauxlib.h

Hope that helps :)
Terradigits
Terradigits
Sorry guys, but I still cant get it working. Neither 4.0 or 5.0 will work. It keeps saying lua_open() is undefined.
Kylotan
Kylotan
Someone did post above suggesting that you don't use lua_open. You haven't told us which version of Lua you're using, either. Lua changes a lot from version to version so it's an important detail.
Terradigits
Terradigits
Well, i've tried both versions. Lua_open() won't work on v4, and the equivalant won't work on v5
Terradigits
Terradigits
Well, I think I have everything worked out now. However, I still get one error during the linking process. Here is my log file:

------ Build started: Project: luatest, Configuration: Debug Win32 ------
Linking...
LINK : fatal error LNK1104: cannot open file 'uuid.lib'
Build log was saved at "file://c:\Documents and Settings\xxx\My Documents\Visual Studio 2005\Projects\luatest\luatest\Debug\BuildLog.htm"
luatest - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
EmrldDrgn
EmrldDrgn
Quote:
Original post by Zorbfish
Quote:
Original post by EmrldDrgn
Actually, I find Lua 4.0 to be much easier than 5.* on the C/++ side of things (I can't seem to find a simple "DoFile" method in 5.*), so you might consider just using 4 for learning purposes. Certainly it'll make that tutorial easier to follow.


In 5.0:
lua_dofile in lauxlib.h

In 5.1:
luaL_dofile in lauxlib.h

Hope that helps :)


Hehe... my bad :). Thanks.

Anyway, I don't know what your problem there might be, but I suggest you make sure you're doing the following:
1) #include-ing lua.hpp instead of extern "C"-ing lua.h. Apart from a timesaver, it grabs the laux files you'll need as well.
2) #pragma comment(lib, lua5.1.1.lib), or adding the .lib as a dependancy in project settings.
3) Only have 1 lua version on your include and libs paths at any given time. Otherwise they'll probably fight.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~I program in C++, on MSVC++ '05 Express, and on Windows. Most of my programs are also for windows.
Terradigits
Terradigits
I tried all of those, none of them worked :(. I still get the same error. I'm starting to think i'll never get this working...
EmrldDrgn
EmrldDrgn
I Google-d around for you real quick, and it appears uuid.lib is in the PSDK, which is used for a lot of stuff. Make sure you (re-)install that, found here.
Should be the correct version, no promises.
EDIT: Linkified... forgot the ""'s. Whoops...

[Edited by - EmrldDrgn on February 23, 2007 8:03:37 PM]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~I program in C++, on MSVC++ '05 Express, and on Windows. Most of my programs are also for windows.
Zorbfish
Zorbfish
Emerald:
http://www.gamedev.net/community/forums/faq.asp

Terradigits:
I do not think uuid.lib is a problem having to do with Lua.

Just as a quick question:
Did you compile the version of the Lua library you are using or did you use a prebuilt binary?

How about trying one of the standard binaries here with the code everyone has suggested and see if that works for you. You want to download the lua5_1_1_Win32_dll8_lib.zip distributable for VC++2k5.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.