#Build library and use in another zig project

1 messages · Page 1 of 1 (latest)

orchid lantern
#

Hi guys,
I am trying to find any information about compiling my library project and using its output library in another. Can you please share some keywords to google it )

#

Please, help

pure nova
#

It's called copy/pasting. Learn from a master.

Zigistry is a package manager for zig with packages.

Go onto there find some packages, or go onto GitHub directly and search for zig projects, maybe wrappers. They often include an example on how to use it or they use libraries in their package.

orchid lantern
#

@pure nova Most of the projects are opensource and they don't ship libraries, they just use other opensource github projects in their dependencies

#

in build.zig.zon file

pure nova
#

Yeah, what's wrong with that?

orchid lantern
#

That's not what I want. Let's say I have two seperate projects A and B. A is a library and B is executable. I want A to build dll, so, or some other library binary and use it in project B without source code

wise coral
orchid lantern
#

Well, maybe minimal source code

orchid lantern
#

C and C++ have headers. Does zig do the same? I figured that similar thing is needed

pure nova
#

I never needed to create a shared library and use it in zig code before

I once created a .dll with:

const lib = b.addSharedLibrary(.{
        .name = ...

which just did some DllMain magic

orchid lantern
#

Do I need a dll, though? I'm new to zig and other machine code compiling languages. Does compile to other file formats (I don't really know how they are called). Will I have to use something like pub extern "c" fn in my project B?

orchid lantern
pure nova
#

I have no idea. so both projects are in zig? is there a good reason for why you want to build and then link it? exporting all the functions as c code should work but I dont see a reason when you use your library in another zig project

orchid lantern
pure nova
wise coral
#

the best way to share zig code is using a module, rather than a library

// project 1 build.zig
b.addModule("project1", .{ .root_source_file = b.path("src/root.zig", .target = target, .optimize = optimize });

// project 2 build.zig:
const project1_dep = b.dependency("project1", .{.target = target, .optimize = optimize});
exe.root_module.addImport("project1", project1_dep.module("project1");

// project 2 build.zig.zon
.{
    .name = "project2",
    .version = "0.1.0",
    .paths = .{""},
    .dependencies = .{
        .project1 = .{ .path = "../project1" }, // or from a url
    },
}

this shares source code rather than a compiled library. If you would rather share using a compiled library, you will have to make functions export ie export fn somefn() i32 {return 1}, and on the other side import them using extern fn somefn() i32;. That could go in a module to make it simple.

To export the library, you would use b.installArtifact(mylib) in the first build.zig and exe.linkLibrary(project1_dep.artifact("library")) in the second

orchid lantern
pure nova
wise coral
#

if you don't want the first project to have source code at all, you'll have to build the library and copy out the bindings file and then use exe.addLibraryPath(project1_dep.path(".")); exe.linkSystemLibrary("library_name")

#

or you can make an object file and use .addObjectFile(project1_dep.path("library_name.o"))

orchid lantern
wise coral
#

only the ones you want to share

pure nova
#

I think it should be possible to create one file where you can put in all exports, or at the place where you export. if you dont want have the export be part of the fn definition directly

// Export Data
comptime {
    @export(DllMain, .{ .name = "DllMain", .linkage = .strong });
}