Skip to content

Lack of official documentation for creating shared modules #147

@yiv1

Description

@yiv1

lua.zig

const std = @import("std");
const zlua = @import("zlua");
const Lua = zlua.Lua;

fn add(L: *Lua) i32 {
    const arg = L.toInteger(1) catch 0;

    std.debug.print("Argument from Lua: {}\n", .{arg});

    L.pushInteger(arg + 1);

    return 1;
}

fn module(lua: *Lua) i32 {
    const functions = [_]zlua.FnReg{
        zlua.FnReg{ .name = "add", .func = zlua.wrap(add) },
    };

    Lua.newLib(lua, &functions);
    // lua.registerFns("lualib", &functions);

    return 1;
}

comptime {
    _ = zlua.exportFn("lualib", module);
}

build.zig

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});

    const moduleLua = b.createModule(.{
        .root_source_file = b.path("./src/lua.zig"),
        .target = target,
        .optimize = .ReleaseFast,
    });

    const libraryLua = b.addLibrary(.{
        .linkage = .dynamic,
        .name = "lualib",
        .root_module = moduleLua,
        .use_llvm = true,
    });

    libraryLua.addIncludePath(b.path("./src/lua-5.4.1/src"));
    libraryLua.addLibraryPath(b.path("./src/lua-5.4.1"));
    libraryLua.linkSystemLibrary("lua54"); // lua54.dll ?

    const zlua = b.dependency("zlua", .{
        .target = target,
        .optimize = .ReleaseFast,
        .lang = .lua54,
        .shared = true,
    });
    libraryLua.root_module.addImport("zlua", zlua.module("zlua"));

    b.installArtifact(libraryLua);
}

build.zig.zon

.{
    .name = ***,
    .version = "0.0.0",
    .fingerprint = ****,
    .minimum_zig_version = "0.14.0",
    .dependencies = .{
        .zlua = .{
            .url = "git+https://github.com/natecraddock/ziglua#891945ef9c2bffcd06050c4836d414428ed7d95a",
            .hash = "zlua-0.1.0-hGRpC2rgBACFhBjc9s9dGN37ZaTu7rNVZZqduwitv4O7",
        },
    },
    .paths = .{
        "build.zig",
        "build.zig.zon",
        "src",
    },
}

zig build - is running successfully, but

test.lua

local lualib = require("lualib")

local result = lualib.add(42)
print("Result:", result)

Executing the script

..\src\lua-5.4.1\lua54.exe test.lua
C:\***\lua-5.4.1\lua54.exe: error loading module 'lualib' from file '.\lualib.dll':
        The specified procedure could not be found.

stack traceback:
        [C]: in ?
        [C]: in function 'require'
        test.lua:1: in main chunk
        [C]: in ?

Additional information:

PS C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.37.32822\bin\Hostx64\x64> .\dumpbin.exe /imports "c:\***\lualib.dll"
Microsoft (R) COFF/PE Dumper Version 14.37.32825.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file c:\***\lualib.dll

File Type: DLL

  Section contains the following imports:

    lua54.dll
             1800277C0 Import Address Table
             1800276A8 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                           0 luaL_checkstack
                           0 luaL_checkversion_
                           0 lua_createtable
                           0 lua_pushcclosure
                           0 lua_pushinteger
                           0 lua_setfield
                           0 lua_settop
                           0 lua_tointegerx

    lua.dll
             180027808 Import Address Table
             1800276F0 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                           0 _GetPEImageBase
                           0 __main
                           0 __mingw_GetSectionCount
                           0 __mingw_GetSectionForAddress
                           0 __mingw_TLScallback

    api-ms-win-crt-runtime-l1-1-0.dll
             180027838 Import Address Table
             180027720 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                           0 _execute_onexit_table
                           0 _exit
                           0 _initialize_onexit_table
                           0 _initterm
                           0 _initterm_e
                           0 _register_onexit_function
                           0 abort

    KERNEL32.dll
             180027878 Import Address Table
             180027760 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                           0 AcquireSRWLockExclusive
                           0 GetLastError
                           0 ReleaseSRWLockExclusive
                           0 Sleep
                           0 VirtualProtect
                           0 VirtualQuery
                           0 WriteFile

    api-ms-win-crt-stdio-l1-1-0.dll
             1800278B8 Import Address Table
             1800277A0 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                           0 __acrt_iob_func
                           0 __stdio_common_vfprintf
                           0 fwrite

  Summary

        1000 .buildid
        2000 .data
        2000 .pdata
        6000 .rdata
        1000 .reloc
       23000 .text
        1000 .tls
PS C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.37.32822\bin\Hostx64\x64> .\dumpbin.exe /exports "c:\***\lualib.dll"
Microsoft (R) COFF/PE Dumper Version 14.37.32825.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file c:\***\lualib.dll

File Type: DLL

  Section contains the following exports for lualib.dll

    00000000 characteristics
           0 time date stamp
        0.00 version
           1 ordinal base
           2 number of functions
           2 number of names

    ordinal hint RVA      name

          1    0 00001000 _DllMainCRTStartup
          2    1 00001010 luaopen_lualib

  Summary

        1000 .buildid
        2000 .data
        2000 .pdata
        6000 .rdata
        1000 .reloc
       23000 .text
        1000 .tls

Test folder structure:

  • lua.dll
  • lua54.dll
  • lualib.dll
  • test.lua

If i remove lua.dll, i got

..\src\lua-5.4.1\lua54.exe test.lua
C:\***\lua-5.4.1\lua54.exe: error loading module 'lualib' from file '.\lualib.dll':
        The specified module could not be found.

stack traceback:
        [C]: in ?
        [C]: in function 'require'
        test.lua:1: in main chunk
        [C]: in ?

I'm confusing. What should I do to get the dll to work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions