在Windows上使用gcc 5.3.0编译Lua 5.2.4模块 [英] Compile Lua 5.2.4 Module with gcc 5.3.0 on Windows
问题描述
我需要用gcc 5.3.0编译一个Lua 5.2.4模块。在这之前,我通过以下步骤构建了Lua 5.2.4:
<$ p $
gcc -c -DLUA_BUILD_AS_DLL * .c
ren lua.o lua.obj
ren luac.o luac.obj
ar rcs lua524-static.lib * .o
gcc -shared -o lua524.dll -Wl, - out-implib,lua524.lib * .o
gcc lua.obj lua524.lib -o lua524.exe
gcc luac.obj lua524-static.lib -o luac524.exe
del * .o * .obj
我想创建的动态库(.dll)写在下面(我们称之为LuaMath):
#包括< windows.h>
#include< math.h>
#includelauxlib.h
#includelua.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);对于(j = 1; j <= n; j ++)
{
lua_pushnumber(L,j); / * i行(i)tabOUT n * /
; / * 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);
}
/ *将0/1放入
行(i)中的j
位置* /
lua_settable(L,-4); / * i行(i)tabOUT n * /
}
lua_insert(L,-2);将行(i)插入到tabOUT中的
位置* /
lua_settable(L,2); / *行(i)我tabOUT n * /
/ / * tabOUT n * /
}
return 1;
static const struct luaL_Reg LuaMath [] = {{IdentityMatrix,IdentityMatrix},
{NULL,NULL}};
LUA_API int luaopen_LuaMath(lua_State * L)
{
luaL_newlib(L,LuaMath);
返回1;
}
正如这里,我建立上面的代码如下:
gcc -O2 -c -DLUA_BUILD_AS_DLL -o LuaMath.o LuaMath.c
gcc -O -shared -o LuaMath.dll LuaMath.o -L。 -llua524
当我运行以下Lua代码时:
require(LuaMath)
A = LuaMath.IdentityMatrix(2)
输出错误是:
stdin:1:尝试索引全局'LuaMath' (无值)
栈回溯:
stdin:1:在主块
[C]:in?
我做错了什么?
在此先感谢
您的C代码是正确的。通常的Lua习语是
LuaMath = require(LuaMath)
$ b如果你想把你的库加载到一个全局变量中。
如果你想要一个局部变量,使用
local LuaMath = require(LuaMath)
I need to compile a Lua 5.2.4 module with gcc 5.3.0. on Windows.
Before doing that, I build the Lua 5.2.4 in the following steps:
gcc -c -DLUA_BUILD_AS_DLL *.c ren lua.o lua.obj ren luac.o luac.obj ar rcs lua524-static.lib *.o gcc -shared -o lua524.dll -Wl,--out-implib,lua524.lib *.o gcc lua.obj lua524.lib -o lua524.exe gcc luac.obj lua524-static.lib -o luac524.exe del *.o *.obj
The dynamic library (.dll) I want to create is written in the following (let's call it 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}}; LUA_API int luaopen_LuaMath(lua_State *L) { luaL_newlib(L,LuaMath); return 1; }
As stated here, I build the above code as follow:
gcc -O2 -c -DLUA_BUILD_AS_DLL -o LuaMath.o LuaMath.c gcc -O -shared -o LuaMath.dll LuaMath.o -L. -llua524
when I run the following Lua code:
require("LuaMath") A=LuaMath.IdentityMatrix(2)
the output error is:
stdin:1: attempt to index global 'LuaMath' (a nil value) stack traceback: stdin:1: in main chunk [C]: in ?
What do I do wrong?
Thanks in advance
解决方案Your C code is correct. The usual Lua idiom is
LuaMath=require("LuaMath")
if you want to load your library into a global variable.
If you want a local variable, use
local LuaMath=require("LuaMath")
这篇关于在Windows上使用gcc 5.3.0编译Lua 5.2.4模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!