Module DLL with Lua 5.4.2

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Module DLL with Lua 5.4.2

Ranier Vilela-2
Hi,

I'm trying to create a module in DLL, with msvc 2019 (64 bits).
When I call the C function inside Lua, it is giving the following error:
lua: atests.lua: 6: attempt to call a nil value (global 'atest1')
stack traceback:
         atests.lua: 6: in main chunk
         [C]: in?

Do I need something more to create the DLL to work with Lua?
DEFINES=/D "_WIN32" /D "DEBUG" /D "_CONSOLE" /D "_MBCS" /D "_CRT_SECURE_NO_WARNINGS" /D "LUA_BUILD_AS_DLL"
LDFLAGS=/implib:libtest.lib /out:libtest.dll /verbose /subsystem:console /debug
link /DLL $(LDFLAGS) lua5.4.2-static.lib *.obj

regards,
Ranier Vilela
Reply | Threaded
Open this post in threaded view
|

Re: Module DLL with Lua 5.4.2

Sergey Kovalev
Why did you use lua5.4.2-static.lib for dll ?

вт, 27 окт. 2020 г. в 14:22, Ranier Vilela <[hidden email]>:

>
> Hi,
>
> I'm trying to create a module in DLL, with msvc 2019 (64 bits).
> When I call the C function inside Lua, it is giving the following error:
> lua: atests.lua: 6: attempt to call a nil value (global 'atest1')
> stack traceback:
>          atests.lua: 6: in main chunk
>          [C]: in?
>
> Do I need something more to create the DLL to work with Lua?
> DEFINES=/D "_WIN32" /D "DEBUG" /D "_CONSOLE" /D "_MBCS" /D "_CRT_SECURE_NO_WARNINGS" /D "LUA_BUILD_AS_DLL"
> LDFLAGS=/implib:libtest.lib /out:libtest.dll /verbose /subsystem:console /debug
> link /DLL $(LDFLAGS) lua5.4.2-static.lib *.obj
>
> regards,
> Ranier Vilela
Reply | Threaded
Open this post in threaded view
|

Re: Module DLL with Lua 5.4.2

Ranier Vilela-2
Em ter., 27 de out. de 2020 às 09:06, Sergey Kovalev <[hidden email]> escreveu:
Why did you use lua5.4.2-static.lib for dll ?
Because, lua functions (ABI) are used inside DLL module.
 
regards,
Ranier Vilela
Reply | Threaded
Open this post in threaded view
|

Re: Module DLL with Lua 5.4.2

Sergey Kovalev
Try to write: attest.lua
function dump(x) for k,v in pairs(x) do print(k,v) end end
dump(_G);
I think you just do not register your function in global variables.
In general if you write a module for lua it should use dynamic
linking. So you could use it with a simple require statement.

For example using tcc (
http://download.savannah.gnu.org/releases/tinycc/ and
https://www.lua.org/ftp/lua-5.3.5.tar.gz )
[build-lua.cmd]
set src=lua-5.3.5\src
tcc -o lua.dll -I%src% -shared -DLUA_BUILD_AS_DLL ^
    %src%\lauxlib.c ^
    %src%\lbaselib.c ^
    %src%\lbitlib.c ^
    %src%\lcorolib.c ^
    %src%\ldblib.c ^
    %src%\liolib.c ^
    %src%\lmathlib.c ^
    %src%\loslib.c ^
    %src%\lstrlib.c ^
    %src%\ltablib.c ^
    %src%\lutf8lib.c ^
    %src%\loadlib.c ^
    %src%\linit.c ^
    %src%\lapi.c ^
    %src%\lcode.c ^
    %src%\lctype.c ^
    %src%\ldebug.c ^
    %src%\ldo.c ^
    %src%\ldump.c ^
    %src%\lfunc.c ^
    %src%\lgc.c ^
    %src%\llex.c ^
    %src%\lmem.c ^
    %src%\lobject.c ^
    %src%\lopcodes.c ^
    %src%\lparser.c ^
    %src%\lstate.c ^
    %src%\lstring.c ^
    %src%\ltable.c ^
    %src%\ltm.c ^
    %src%\lundump.c ^
    %src%\lvm.c ^
    %src%\lzio.c

tcc -o lua.exe -I%src% -DLUA_BUILD_AS_DLL -L. lua.def ^
    %src%\lua.c

[example.c]
#include <windows.h>
#include "lauxlib.h"

/* Pop-up a Windows message box with your choice of message and caption */
int lua_msgbox(lua_State* L) {
    const char* message = luaL_checkstring(L, 1);
    const char* caption = luaL_optstring(L, 2, "");
    int result = MessageBox(NULL, message, caption, MB_OK);
    lua_pushnumber(L, result);
    return 1;
}

int __declspec(dllexport) luaopen_example(lua_State* L) {
    lua_register(L, "msgbox",  lua_msgbox);
    return 0;
}

[build-example.cmd]
set inc=lua-5.3.5\src
tcc -o example.dll -L. -l%inc% -Ilua -shared example.c

[example.lua]
require "example"
msgbox("hello","example.dll")

Run with: lua.exe example.lua

вт, 27 окт. 2020 г. в 15:18, Ranier Vilela <[hidden email]>:
>
> Em ter., 27 de out. de 2020 às 09:06, Sergey Kovalev <[hidden email]> escreveu:
>>
>> Why did you use lua5.4.2-static.lib for dll ?
>
> Because, lua functions (ABI) are used inside DLL module.
>
> regards,
> Ranier Vilela
Reply | Threaded
Open this post in threaded view
|

Re: Module DLL with Lua 5.4.2

Ranier Vilela-2

Em ter., 27 de out. de 2020 às 10:29, Sergey Kovalev <[hidden email]> escreveu:
Try to write: attest.lua
function dump(x) for k,v in pairs(x) do print(k,v) end end
dump(_G);
Thanks for the hint.

Added the line require "libtest", I don't know if it made a difference.
The dump suggested, show my module functions:
testfuncs  table: 000001A9740D72A0
like
io      table: 000001A9740D1780

Why Lua 5.4, do not find it in "testfuncs table"?
 
I think you just do not register your function in global variables.
In general if you write a module for lua it should use dynamic
linking. So you could use it with a simple require statement.
Well, I am registering like this.

libtest.c
static const luaL_Reg testsfuncs_lib[] = {
  {"atest1", atest1},
  {"atest2", atest2},
  {NULL, NULL}
};

LUAMOD_API int luaopen_testsfuncs (lua_State *L) {
  luaL_newlib(L,  testsfuncs_lib);
  return 1;
}

testinit.c
static const luaL_Reg loadedlibs[] = {
  {"atestsfuncs", luaopen_testsfuncs },
  {NULL, NULL}
};

LUALIB_API int luaopen_libtest(lua_State *L) {
    const luaL_Reg *lib;

    for (lib = loadedlibs; lib->func; ++lib) {
       luaL_requiref(L, lib->name, lib->func, 1);
       lua_pop(L, 1);
    }

return 1;
}

This is the way with 5.4?
 

For example using tcc (
http://download.savannah.gnu.org/releases/tinycc/ and
https://www.lua.org/ftp/lua-5.3.5.tar.gz )
Unfortunately, I have to use msvc.
 
int __declspec(dllexport) luaopen_example(lua_State* L) {
    lua_register(L, "msgbox",  lua_msgbox);
    return 0;
}
This is pre 5.4, works with 5.4 too?

Thanks for helping me with this trouble.

regards,
Ranier Vilela
Reply | Threaded
Open this post in threaded view
|

Re: Module DLL with Lua 5.4.2

Ranier Vilela-2
Em ter., 27 de out. de 2020 às 14:26, Ranier Vilela <[hidden email]> escreveu:

Em ter., 27 de out. de 2020 às 10:29, Sergey Kovalev <[hidden email]> escreveu:
Try to write: attest.lua
function dump(x) for k,v in pairs(x) do print(k,v) end end
dump(_G);
Thanks for the hint.

Added the line require "libtest", I don't know if it made a difference.
The dump suggested, show my module functions:
testfuncs  table: 000001A9740D72A0
like
io      table: 000001A9740D1780

Why Lua 5.4, do not find it in "testfuncs table"?
Solved. In Lua program, with functions stored in table, need the namespace:
table.function

Thank you Sergey Kovalev.

Ranier Vilela