如何在 Visual Studio 2010 中为链接器编译文件。
这些是我在 Visual Studio 2010 中使用 Lua 5.2.1 源代码所遵循的步骤:
- cl /MD /O2 /c /DLUA_BUILD_AS_DLL *.c
- 用扩展名“.o”重命名“lua.obj”和“luac.obj”,这样它们就不会被选中
- 链接 /DLL /IMPLIB:lua5.2.lib /OUT:lua5.2.dll *.obj
- 链接 /OUT:lua.exe lua.o lua5.2.lib
- lib /OUT:lua5.2-static.lib *.obj
- 链接 /OUT:luac.exe luac.o lua5.2-static.lib
从上述所有步骤中,我选择了 lua5.2.lib 并将其添加到链接器中,如下所示:
当我使用 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'.
有什么帮助吗?