Question

Say I have a lua file:

--functions.lua
function testadd(a, b) 
    return a+b
end

How would I use luabind to load that file, and call that function- something like:

//test.cpp
extern "C" {
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
}
#include <luabind/luabind.hpp>
#include <luabind/function.hpp> 

int main() {
    lua_State *myLuaState = lua_open();
    luaL_openlibs(myLuaState);
    luaL_loadfile(myLuaState, "functions.lua");
    luabind::open(myLuaState);
    int value = luabind::call_function<int>(myLuaState, "testadd", 2, 3);
    lua_close(myLuaState);
}

But this returns an error: terminate called after throwing an instance of 'luabind::error' what(): lua runtime error Aborted

So, what is the proper syntax for doing what I want to do? (From the looks of the error it seems to be a problem with the syntax in the lua file, but I don't think it is...)

Was it helpful?

Solution

You probably want to call luaL_dofile instead of luaL_loadfile here.

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