1

I'm trying to bind Lua in my applications, now I trying to test how bind Lua into C++. My problem is very strange because I want to call function main() from script at start, after luaL_loadfile. My code:

#include <iostream>
#include <cstdlib>
#include <stdio.h>

#include "lua.hpp"
#include "lauxlib.h"
#include "lualib.h"

using namespace std;

int main(int argc, char **argv) {
    lua_State* lua = luaL_newstate();
    luaL_openlibs(lua);

    if (luaL_loadfile(lua, "test.lua") != 0) {
        std::cout << lua_tostring(lua, -1) << "\n";
        lua_pop(lua, 1);
        return 1;
    }

    lua_getfield(lua, LUA_REGISTRYINDEX, "main");
    if (lua_pcall(lua, 0, 1, 0) != 0) {
        printf("Error running function 'main': %s\n", lua_tostring(lua, -1));
        return 1;
    }

    lua_close(lua);

    return 0;
}

and my output is:

Error running function 'main': attempt to call a nil value

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
zbigniewcebula
  • 1,610
  • 2
  • 12
  • 9
  • 1
    What you're trying to do makes absolutely no sense. `main` is called by the C/C++ runtime library as the entry point into your program. Why would you want to call it again from a lua script?? – greatwolf Aug 22 '13 at 00:33
  • Why would you just assume that the [Lua registry](http://www.lua.org/manual/5.1/manual.html#3.5) would have something under the key "main"? Do you understand what the Lua runtime is (and is not)? – remram Aug 23 '13 at 05:11

2 Answers2

2

I put together an example of embedding lua 5.2 into a Visual Studio 2005 console project in this article with source, Extending a C++ Application with Lua 5.2.

It really does not make sense for you to call main() from your lua script since main() is the entry point for your application and there is Lua initialization stuff that you need to do and only to do once.

The example and the article I put together shows using Lua 5.2 with C++ and being able to call C functions you create from your Lua script. The example also shows calling Lua functions from the C++ program as well.

I did this about a year ago using Visual Studio 2005. I suspect going from Visual Studio 2005 to 2012 would be straightforward. I am not sure how straightforward it would be to port to another compiler and development environment. At a minimum the _tmain() function used with a Windows console application in Visual Studio 2005 would need to be renamed to main().

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
  • There is way to load any defined functions from script without `luaL_dofile`? – zbigniewcebula Aug 22 '13 at 01:05
  • My experience has been that I have Lua chunks that are stored in one or more files. I load the various Lua chunks into the Lua engine where they are then available to be used, for example by using lua_pcall() or similar function, from within the C/C++ application. I am not sure what it is that you are trying to accomplish so more details on that would be nice. Typically people use Lua engine along with an application and provide various functions that a Lua script can call to script or automate functionality within the application. – Richard Chambers Aug 22 '13 at 19:43
  • "There is way to load any defined functions from script without luaL_dofile" `function() ... end` is a *statement*, the code must be executed to produce a function value. – Textmode Aug 26 '13 at 05:43
2

Try luaL_dofile instead of luaL_loadfile.

This is one of the most frequent mistakes: luaL_loadfile loads the file but does not run it; it just leaves on the stack a function representing the loaded file, ready to be called. The function main will only be defined when the script is run, that is, when the script function is called. The error message is trying to tell you so. (I assume that your script defines a function called main. There isn't any real need for that, but it is harmless.)

lhf
  • 70,581
  • 9
  • 108
  • 149