在C++中,使用luabind,调用lua文件中定义的函数?
假设我有一个 lua 文件:
--functions.lua
function testadd(a, b)
return a+b
end
我将如何使用 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);
}
但这会返回一个错误: 抛出“luabind::error”实例后调用终止 What(): lua运行时错误 中止
那么,做我想做的事情的正确语法是什么? (从错误的外观看来,这似乎是lua文件中语法的问题,但我不认为这是......)
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...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想在此处调用
luaL_dofile
而不是luaL_loadfile
。You probably want to call
luaL_dofile
instead ofluaL_loadfile
here.