可以在Lua中加载C ++ dll文件吗? [英] Can a C++ dll file be loaded in Lua?

查看:258
本文介绍了可以在Lua中加载C ++ dll文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Lua中加载一个DLL文件来连接不同的API。我知道C类型的dll可以加载,但我有一个dll文件在C + +。

I need to load a DLL file in Lua to connect different APIs. I know that C type dlls can be loaded, but what I have is a dll file produced in C++.

生成此库的代码(在C ++中)的格式为:

The code (in C++) that produced this library was of the form:

// MyAPI.h

namespace MyAPI
{
    public class MyFirstClass
    {
        public: 
           MyFirstClass();
           void performSomeMethod(int arg);
    }
}

然后生成dll文件 MyAPI.dll 。当我现在尝试导入这个在Lua,使用:

which then produced the dll file MyAPI.dll. When I now try to import this in Lua, using:

require "MyAPI"

它立即给出错误:错误加载模块'MyAPI'从文件'.\MyAPI.dll':指定找不到过程。我不明白这是什么意思,或者如何摆脱它。一般来说,C ++库通常不会被Lua包含(也就是说,我应该写另一个C包装吗?)还是有办法做到这一点?

it immediately gives the error: error loading module 'MyAPI' from file '.\MyAPI.dll': The specified procedure could not be found. I do not understand what this means, or how to get rid of it. Can C++ libraries in general not be included by Lua (i.e. should I write another C wrapper?) or is there a way to do this?

推荐答案

是的,可以做到。公开一个C函数装载器 luaopen_MyAPI ,其中可以调用使用任何类型的C ++ Lua包装器的函数,例如 LuaBridge LuaBind 或其他。如果你在C ++中的调用不符合绑定的规则,如生命周期管理,按值传递对象等,你可能需要将类包装成可绑定的类。

Yes, it can be done. Expose a C-function loader luaopen_MyAPI, where you can call a function that uses any kind of C++ Lua Wrapper, such as LuaBridge, LuaBind or others. If your calls in C++ don't conform to the rules of the bindings, such as lifetime management, passing objects by value, etc, you might need to wrap the classes into bindable classes.

有关示例,请参见 pugilua


  • pugilua_lib.h - 模块载入器API

  • pugilua_lib.cpp - 包装类和LuaBridge绑定

  • pugilua.cpp - 调用模块加载器的绑定

这篇关于可以在Lua中加载C ++ dll文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆