本文介绍了在C ++中,使用luabind,在lua文件中定义的调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个lua文件:
- functions.lua
function testadd(a,b)
return a + b
end
如何使用luabind加载文件,并调用该函数 - 例如:
// test.cpp
externC{
#includelua.h
#includelualib.h
#includelauxlib.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文件中的语法问题,但我不认为是...)
解决方案
这里可以调用 luaL_dofile
,而不是 luaL_loadfile
/ p>
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 errorAborted
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...)
解决方案
You probably want to call luaL_dofile
instead of luaL_loadfile
here.
这篇关于在C ++中,使用luabind,在lua文件中定义的调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!