[英]In C++, using luabind, call function defined in lua file?
Say I have a lua file:假设我有一个 lua 文件:
--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:我将如何使用 luabind 加载该文件,并调用该函数 - 类似于:
//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但这会返回一个错误:在抛出 'luabind::error' what() 的实例后调用终止:lua 运行时错误已中止
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...)
(从错误的外观来看,lua 文件中的语法似乎有问题,但我认为不是......)
You probably want to call luaL_dofile
instead of luaL_loadfile
here.你可能想在这里调用
luaL_dofile
而不是luaL_loadfile
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.