编译链接器文件,以将 C 代码嵌入到 Lua 5.2.1 中。

如何在 Visual Studio 2010 中为连接器编译文件。

以下是我在 Visual Studio 2010 中使用 Lua 5.2.1 源代码所遵循的步骤:

  1. 使用以下命令 cl /MD /O2 /c /DLUA_BUILD_AS_DLL *.c 进行编译
  2. 重命名 "lua.obj" 和 "luac.obj" 文件以 ".o" 做为扩展名,以避免它们被选择
  3. 使用以下命令进行链接:link /DLL /IMPLIB:lua5.2.lib /OUT:lua5.2.dll *.obj
  4. 使用以下命令进行链接:link /OUT:lua.exe lua.o lua5.2.lib
  5. 使用以下命令进行链接:lib /OUT:lua5.2-static.lib *.obj
  6. 使用以下命令进行链接:link /OUT:luac.exe luac.o lua5.2-static.lib

从以上所有步骤中,我选择其中的 lua5.2.lib 并将其添加到链接器中,如下所示:

enter image description here

但是,当我使用 Dev-C++ 编译以下 C 代码时,并不会出现任何错误。但是,当我使用 require 命令从 Lua 中调用它时,它说它找不到。

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

#define LUA_LIB   int __declspec(dllexport)

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 */
        lua_settable(L,2);            /*                tabOUT n */
    }
    return 1;
}

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

LUA_LIB luaopen_LuaMath(lua_State *L)
{
    luaL_newlib(L,LuaMath);
    return 1;
}

当我运行以下简单 Lua 代码时:

require("LuaMath")

A=LuaMath.IdentityMatrix(4)

错误消息写道

error loading module 'LuaMath' from file './LuaMath.dll': impossible to find the specified module'.

有什么帮助吗?

点赞