1

I am unable to include a Lua file in another Lua file. I am compiling Lua using C++ and LuaBridge. Here is my main function. Very very small.

    lua_State* luaState = luaL_newstate();
    luaL_openlibs(luaState);
    luaL_dofile(luaState, "res/script.lua");

    LuaRef mainFunction = luabridge::getGlobal("start", luaState);

    try {
        mainFunction();
    } catch (const luabridge::LuaException& e) {
        std::cout << e.what();
    }

    lua_close(luaState);
    std::cin.get();

Here is my "script.lua" file:

mymathmodule = require("mymath")

main = function()
end

And the "mymath.lua" which is in the SAME directory.

mymath =  {
    add = function(a, b)
        return a + b
    end
}

return mymath

The program just crashes. Just removing the "mymathmodule = require("mymath")" line gives no error. What is the problem here??

  • What is the specific error you are getting? `module 'mymath' not found`? – Nifim May 21 '19 at 17:44
  • You're calling a function named `start`but your script defines `main`. – lhf May 22 '19 at 00:08
  • `mymath` is in directory `res/` but most probably `res/` in not in the path that `require` uses. – lhf May 22 '19 at 00:09
  • Actually, I don't have an error checking technique, because the documentation does not explain exception handling. And yes, I changed "start" to main in the C++ code, but the problem still occurs. How can I get the path that require uses? – ReluctantProgrammer May 22 '19 at 03:36
  • It just worked when I changed ```require```'s argument from "module" to "res.module"... How on earth can I find out ```require```'s path? – ReluctantProgrammer May 22 '19 at 03:41
  • You can add `res\?.lua` to the package path variable https://www.lua.org/pil/8.1.html – Nifim May 22 '19 at 05:14

0 Answers0