本文介绍了LuaBind和package.loadlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过luabind在本教程中进行学习, http://www .rasterbar.com/products/luabind/docs.html ,但是我在加载该库时遇到了麻烦.我当前正在使用lua的5.1版本,所以我相信我会使用package.loadlib而不是loadlib.我制作了一个简单的dll:

I'm trying to go through the tutorial with luabind here, http://www.rasterbar.com/products/luabind/docs.html, however i'm having trouble loading the library. I'm currently using version 5.1 of lua, so I believe I would use package.loadlib instead of loadlib. I made a simple dll which is this:

#include <iostream>
#include <luabind\luabind.hpp>

void greet()
{
std::cout << "Hello world!\n";
}

extern "C" int init(lua_State* L)
{
luabind::open(L);

luabind::module(L)
    [
        luabind::def("greet", &greet)
    ];


return 0;
}

这很好.但是,当我尝试运行以下代码时,我在lua中遇到错误:

This builds just fine. However I get an error in lua when I try to run this code:

package.loadlib("LuaTestLib.dll", "init")
greet()

它说问候是零.如何从dll正确加载功能?

It states that greet is nil. How do I load the functions from the dll properly?

推荐答案

来自 package.loadlib的文档:

(添加了重点)

这不会执行 funcname.它只是将其作为函数返回以供您调用.您仍然必须调用它:

This doesn't execute funcname. It simply returns it as a function for you to call. You still have to call it:

package.loadlib("LuaTestLib.dll", "init")()

这篇关于LuaBind和package.loadlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 19:08