我正在尝试编写外部Lua模块.
我在Windows 8.1上工作,我使用gcc作为编译器.
我的要求是自己构建/编译所有内容,而无需使用在线提供的预编译文件.
首先,我构建了Lua 5.2.4的C源代码如下:
gcc -c*.c
ren lua.o lua.obj
ren luac.o luac.obj
ar rcs luaX.XXlib*.o
gcc -shared -o luaX.XXdll*.o
gcc lua.c luaX.XXlib -o luaX.XXexe
gcc luac.c luaX.XXlib -o luacX.XXexe
del*.o*.obj
X.X.X源代码修订在哪里.
一旦我创建了我的.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;
}
Run Code Online (Sandbox Code Playgroud)
然后我编译它链接到动态library.dll如下:
gcc -shared -L "<path where luaX.X.X.dll is>" -l "luaX.X.X" LuaMath.c
Run Code Online (Sandbox Code Playgroud)
当我将模块调用Lua代码时如下:
require("LuaMath")
Run Code Online (Sandbox Code Playgroud)
输出是:
> require("LuaMath")
multiple Lua VMs detected
stack traceback:
[C]: in ?
[C]: in function 'require'
stdin:1: in main chunk
[C]: in ?
>
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
提前谢谢了.