Question

I'm trying for the first time to have Lua embedded in C++. I've been searching for 2 days now, but most of internet tutos use lua5.1, which is incompatible with lua5.2. So I read a bit of lua documentation, example source code, and I end up with this :

main.cpp :

#include "luainc.h"
#include <iostream>

int main(){
    int iErr = 0;
    lua_State *lua = luaL_newstate ();  // Open Lua
    luaopen_io (lua);    // Load io library

    if ((iErr = luaL_loadfile (lua, "hw.lua")) == 0)
    {
       std::cout<<"step1"<<std::endl;

       if ((iErr = lua_pcall (lua, 0, LUA_MULTRET, 0)) == 0)
       { 
          std::cout<<"step2"<<std::endl;

          lua_getglobal (lua, "helloWorld");    // Push the function name onto the stack

            if (lua_type(lua, lua_gettop(lua)) == LUA_TNIL) {
                // if the global variable does not exist then we will bail out with an error.
                std::cout<<"global variable not found : helloworld"<<std::endl;

                /* error so we will just clear the Lua virtual stack and then return
if we do not clear the Lua stack, we leave garbage that will cause problems with later 
function calls from the application. we do this rather than use lua_error() because this function
is called from the application and not through Lua. */

                lua_settop (lua, 0);
                return -1;
            }         

          // Function is located in the Global Table
          /* lua_gettable (lua, LUA_GLOBALSINDEX);  */ //lua5.1
          lua_pcall (lua, 0, 0, 0);
       }
    }

    lua_close (lua);

    return 0;
}

hw.lua :

-- Lua Hello World (hw.lua)
function helloWorld ()
   io.write ("hello World")
end

luainc.h :

#ifndef __LUA_INC_H__
#define __LUA_INC_H__

extern "C"
{
   #include </home/renardc/Documents/Programmation/Lua_CPP/lua-5.2.2/src/lua.h>
   #include </home/renardc/Documents/Programmation/Lua_CPP/lua-5.2.2/src/lauxlib.h>
   #include </home/renardc/Documents/Programmation/Lua_CPP/lua-5.2.2/src/lualib.h>
}

#endif // __LUA_INC_H__

I have no error, the output is :

step1
step2

which should mean that my "helloworld" function has been found. But as I can not see "Hello World" in the output, I suspect that the function has not been called. What am I doing wrong ?

This is how I compile my program :

g++ main.cpp -L/usr/local/include -I/usr/local/include -llua
Was it helpful?

Solution

First, why not #include "lua.hpp", which comes with Lua and does mostly what your luainc.hdoes?

There are two problems with your code:

  1. You don't emit any error message when luadL_loadfile fails.

  2. You use lua_pcall to call helloWorld but does not test its return value.

When you change lua_pcall to lua_call you get this error message:

attempt to index global 'io' (a nil value)

This means that you forgot to set the global io after calling luaopen_io. Just add lua_setglobal(lua,"io")and it works. Unlike Lua 5.1, Lua 5.2 does not set globals automatically when you open libraries, unless the library itself does it, which is discouraged.

You'll probably be better off calling luaL_openlibs to open all standard Lua libraries with no surprises.

You may as well use luaL_dofile instead of luaL_loadfile and save the first lua_pcall. You still have to check the return value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top