# [C++] Lua integration

This topic is 4139 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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

##### Share on other sites
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++

##### Share on other sites
Quote:
 Original post by TerradigitsI 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.aspMy 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.

##### Share on other sites
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!

##### Share on other sites
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.

##### Share on other sites
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;}

##### Share on other sites
If you're binding C++ and LUA I highly suggest using luabind:

http://www.rasterbar.com/products/luabind/docs.html

##### Share on other sites
Quote:
 Original post by TerradigitsWell, 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.

##### Share on other sites
Quote:
 Original post by EmrldDrgnActually, 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 :)

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 37
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
631414
• Total Posts
2999952
×