繁体   English   中英

如何创建Lua模块的DLL

[英]How to create dll of Lua Module

我正在尝试编写外部Lua模块。

我在Windows 8.1上工作,我使用gcc作为编译器。

我的要求是自己构建/编译所有内容,而无需使用在线提供的预编译文件。

首先,我构建了Lua 5.2.4的C源代码如下:

  1. gcc -c * .c

  2. ren lua.o lua.obj

  3. ren luac.o luac.obj

  4. ar rcs luaX.XXlib * .o

  5. gcc -shared -o luaX.XXdll * .o

  6. gcc lua.c luaX.XXlib -o luaX.XXexe

  7. gcc luac.c luaX.XXlib -o luacX.XXexe

  8. del * .o * .obj

其中XXX是源代码修订版。

一旦我创建了我的.exe,我就编写了我的模块的C代码(让我们称之为LuaMath):

#include<windows.h>
#include<math.h>
#include "lauxlib.h"
#include "lua.h"


static int IdentityMatrix(lua_State *L)
{
    int in = lua_gettop(L);
    if (in!=1)
    {
       lua_pushstring(L,"Maximum 1 argument");
       lua_error(L);
    }
    lua_Number n = lua_tonumber(L,1);
    lua_newtable(L);                  /*                 tabOUT n */
    int i,j;
    for (i=1;i<=n;i++)
    {
        lua_newtable(L);              /*         row(i) tabOUT n */
        lua_pushnumber(L,i);          /*       i row(i) tabOUT n */
        for (j=1;j<=n;j++)
        {
            lua_pushnumber(L,j);      /*     j i row(i) tabOUT n */
            if (j==i)
            {
                lua_pushnumber(L,1);
            }
            else                      /* 0/1 j i row(i) tabOUT n */
            {
                lua_pushnumber(L,0);
            }
            /*  Put 0/1 inside row(i) at j position */
            lua_settable(L,-4);       /*       i row(i) tabOUT n */
        }
        lua_insert(L,-2);             /*       row(i) i tabOUT n */

        /* Insert row(i) into position in tabOUT */
        lua_settable(L,2);            /*                tabOUT n */
    }
    return 1;
}


static const struct luaL_Reg LuaMath [] = {{"IdentityMatrix", IdentityMatrix},
                                           {            NULL,           NULL}};

int __declspec(dllexport) luaopen_LuaMath(lua_State *L)
{
    luaL_newlib(L,LuaMath);
    return 1;
}

然后我编译它链接到动态library.dll如下:

gcc -shared -L "<path where luaX.X.X.dll is>" -l "luaX.X.X" LuaMath.c

当我将模块调用Lua代码时如下:

require("LuaMath")

输出是:

> require("LuaMath")
multiple Lua VMs detected
stack traceback:
        [C]: in ?
        [C]: in function 'require'
        stdin:1: in main chunk
        [C]: in ?
>

我做错了什么?

提前谢谢了。

不要将Lua库与DLL链接。 这就是错误消息告诉你的内容。

可能是你需要定义一个SYSCFLAGS LUA_BUILD_AS_DLL然后再次编译lua代码

gcc -DLUA_BUILD_AS_DLL -c *.c

为什么不使用由Lua源提供的Makefile? 这是一种简单的编译方式,如果您阅读Makefile,它会为您设置gcc标志。

cd /path/to/lua-src
make mingw

现在你应该在/ path / to / lua-src / src /中获取lua.exe,luac.exe,lua53.dll

编译你的模块

gcc -shared -I/path/to/lua-src/src LuaMath.c -o LuaMath.dll -L/path/to/lua-src/src -llua53

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM