#Creating global zig library (Linux)

1 messages · Page 1 of 1 (latest)

pine wagon
#

I am trying to create a simple graphics library using OpenGL

what i want to do is to try to use this library in my other projects however some info online is quite outdated so i am not rly able to get this running

my library

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

    const optimize = b.standardOptimizeOption(.{});

    const lib = b.addStaticLibrary(.{
        .name = "maskot",
        .root_source_file = b.path("src/root.zig"),
        .target = target,
        .optimize = optimize,
    });

    b.installArtifact(lib);

    const run_cmd = b.addRunArtifact(lib);

    run_cmd.step.dependOn(b.getInstallStep());

    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);

    lib.linkLibC();
    lib.addIncludePath(b.path("src/vendors"));
    lib.addCSourceFiles(.{ .root = b.path("src/vendors"), .files = &.{"glad.c"} });
    lib.linkSystemLibrary("gl");
    lib.linkSystemLibrary("glfw");
}

root.zig

pub const window = @import("window.zig");
pub const shader = @import("shader.zig");
pub const shape = @import("shape.zig");

other project

    exe.linkLibC();

    const maskot_lib = b.addStaticLibrary(.{
        .name = "maskot",
        .root_source_file = b.path("../maskot/src/root.zig"),
        .target = target,
        .optimize = optimize,
        .use_llvm = false,
        .use_lld = false,
    });
    exe.linkLibrary(maskot_lib);

    exe.linkSystemLibrary("gl");
    exe.linkSystemLibrary("glfw");

    b.installArtifact(exe);
}

const maskot = @import("maskot");

this doesnt work

royal plinth
#

You'll need to be more specific than "doesn't work".

pine wagon
#

wdym more specific?

i want to link my library to another project

#

i am not able to export contents from my root.zig

royal plinth
#

Zig prefers building from zig source than linking libraries. If you want to link a lib, then you need to expose the lib code via extern "c".

pine wagon
#

ty, i will checks some docs for that

#

if not outdated

pine wagon
#
    const maskot_lib = b.addStaticLibrary(.{
        .name = "maskot",
        .root_source_file = b.path("../maskot/src/root.zig"),
        .target = target,
        .optimize = optimize,
    });
    exe.linkLibrary(maskot_lib);

maskot root.zig

export fn idkk() void {
    return idk();
}

pub fn idk() void {}

from zig by examples this should work but after importing it into my project, i am not getting any of the methods
i am also not getting any errors

royal plinth
#

They need to be extern "c", not just export.

pine wagon
#

error: extern func have no body

#

none of the sources used extern "c" not even zig docs

royal plinth
#

Zig is generally not used for static libs unless needed for C interop.

pine wagon
#

how about dynamic 1 then?

#

im going to be using it in another zig project

#

not in c

royal plinth
#

Same -- Zig doesn't have a native ABI, so you use C ABI for static and dynamic libs. If you want to share straight Zig code, then set up .zon and use modules.