#Build library and use in another zig project
1 messages · Page 1 of 1 (latest)
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.
@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
Yeah, what's wrong with that?
https://github.com/Guigui220D/zig-sfml-wrapper
This is a package which does it this way
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
are both projects zig projects with zig code, or does one or the other have c mixed in?
Well, maybe minimal source code
you will still need headers
C and C++ have headers. Does zig do the same? I figured that similar thing is needed
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
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?
Preferebly only zig code in both projects
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
Let's say some commertial project or something like that
so the library code stays hidden? okay ig...
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
What's wrone with that?
nothing, I know some libs that do it like that for obscure reasons.
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"))
Thanks! Does that mean that every function and struct have to be marked with extern or only those that I want to share?
only the ones you want to share
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 });
}