无法在Lua中加载C dll模块 [英] unable to load c dll module in Lua

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

问题描述

我尝试使用"require"在Lua中测试加载c ++ dll模块,以下是c ++模块文件

I try to test loading a c++ dll module in Lua using "require", below is the c++ module file

 #include <stdio.h>
#include <iostream>

extern "C" {
    #include "lua/lualib.h"
    #include "lua/lauxlib.h"
    #include "lua/lua.h"

    __declspec(dllexport) int luaopen_mylib(lua_State* L);
}

using namespace std;

static int libFunc1(lua_State* L)
{
    int n = lua_gettop(L);
    printf("in myfunc stack, arg number: %d\n", n);
    if (lua_isstring(L, -1))
    {
        std::cout << lua_tostring(L, -1) << std::endl;
    }
    else
    {
        std::cout << "invalid arg" << std::endl;
    }
    return 1;
}

static const struct luaL_Reg mylib[] = {{"func1", libFunc1}, {NULL, NULL}};

int luaopen_mylib(lua_State* L)
{
    cout << "loading my lib" << endl;
    luaL_newlib(L, mylib);
    return 1;
}

我在msys中使用g ++将这个cpp文件编译为dll:

I compiled this cpp file into dll using g++ in msys:

g++    -c -o mylib.o mylib.cpp
g++ -shared -o mylib.dll mylib.o -Llua -llua5.3.0

直到现在一切正常,而且我也得到了mylib.dll文件.但是当我尝试加载模块时,出现错误消息:

until now everything work fine, and I got the mylib.dll file too. but when I try to load the module, I got the error msg:

> require("mylib")
error loading module 'mylib' from file '.\mylib.dll':
        找不到指定的程序。

stack traceback:
        [C]: in ?
        [C]: in function 'require'
        stdin:1: in main chunk
        [C]: in ?

上述汉字的意思是:

The specified function could not be found.

我认为指定功能"的意思是"luaopen_mylib",但是cpp文件确实具有以下功能:luaopen_mylib,这会出错吗?

I think the "specified function" mean the "luaopen_mylib", but the cpp file do have the function:luaopen_mylib, WHAT IS GOING WRONG?

推荐答案

这可能是一些名称修改问题.试试:

It might be some name mangling problem. Try:

extern "C" 
{
    int luaopen_mylib(lua_State* L)
    {
        cout << "loading my lib" << endl;
        luaL_newlib(L, mylib);
        return 1;
    }
}

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

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