2

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

Azoun
  • 255
  • 3
  • 14
  • In `luaopen_LuaMath` you should add a line of code to save created library table in global environment (of course, its name "LuaMath" must be provided as a string). – Egor Skriptunoff May 19 '17 at 18:44

1 Answers1

4

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")
lhf
  • 70,581
  • 9
  • 108
  • 149
  • oohh man...right now I feel sooo stupid. I accept your answer as the solution. Many many thanks!!! – Azoun May 19 '17 at 19:18