Build.zig guide for static libraries created in C

System OS: Linux Mint 22 Wilma
Zig version: 0.13.0

file tree before build:
file_tree1

the files:

math.h

#ifndef MATH_H
#define MATH_H

int add(int a, int b);

#ifndef INCREMENT_BY
  #define INCREMENT_BY 1
#endif

int increment(int x);

#endif

lib32_c.a is compiled from this code

#include "math.h";

int add(int a, int b) {
  return a + b;
}

int increment(int x) {
  return x + INCREMENT_BY;
}

main.zig

const std = @import("std");

const math = @cImport({
  @cInclude("math.h");
});

pub fn main() !void {
  const a = 21;
  const b = 31;
  const c = math.add(a,b);

  std.debug.print("a + b => {d}\n", .{c});
  std.debug.print("a++ => {d}\n", .{math.increment(a)});
}

build.zig

const std = @import("std");

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

  const exe = b.addExecutable(.{
    .name = "32_c",
    .link_libc = true,
    .root_source_file = b.path("main.zig"),
    .target = target,
    .optimize = optimize,
  });

  exe.addIncludePath(b.path("./"));
  exe.addLibraryPath(b.path("./lib"));
  exe.linkSystemLibrary("32_c"); // from lib32_c.a to 32_c
  
  b.installArtifact(exe);
}

on shell run {workdirectory}/zig-out/bin/32_c
or ./zig-out/bin/32_c

Never tried this on much more complicated structure but I hope this helps

1 Like

It seems the purpose of Explain Category needs some clarification.

My take is that a first post in a topic in this category
should be more like a request to explain some things,
but not giving an explanation of “some things”,
as if someone asked to explain these “some things”.

I’m really sorry that I placed this on the wrong category.
I started learning zig 2 weeks ago and if there is one thing I’ve noticed is the lack of guides or documentation.
I’ve been dealing with this problem of trying to link static files with C headers but the internet didn’t offer me anything that solves my this problem directly especially on Zig 0.13.0 on a linux machine. So when I kinda found this work around, I decided to place it here since I don’t have any blog website at the moment.
At first I thought that I should place it under Help category but I realized that I’m not looking for help so I sort of tried to place on where I thought it should be.
But I would be more diligent next time.

1 Like

Then probably your post better fits “Docs” category. And welcome to ziggit!

For some reason I can’t post to “Docs” yet.

And thanks.

There are only 3 letters left.
X, Y and Z

Ziggitta? :- :innocent:

I see, thanks again. :sunglasses:

I’ve been able to get complex build systems working using the official build docs. There is only one example of linkSystemLibrary, but it is there. I think more of the focus within the Zig community is in building C sources within the build script rather than linking pre-built libraries. Have you considered compiling math.c within build.zig instead?

I’ve seen a lot of tutorials on how to compile c within build.zig?
However I’m more of like, what do I do if an “unusual scenario like this” happens guy.
I’ve haven’t built something significant using zig lang yet and I’m just tinkering with the tools available. While tinkering it came to me like, what if I found a good framework with only headers and static files on it, then I did some basic scenarios then did some googling and at best I’ve seen a youtube tutorial with the same conditions as I mentioned above but the only problem is the youtuber is windows and a different version of zig so I can’t apply what he did on that video to my machine.
And then had trouble searching for a solution online. I didn’t found a solution which directly solves it so what I tried reducing the whole problem to smaller more specific fragments, find solutions on them then try applying what I learned those small situations merging them back to the bigger problem and then I was able to figure out this solution.
I decided to post it here just in case I found myself on a somewhat similar situation in the future for easier referencing. I have a local folder where I place my notes but it’s nice to have one also online.

1 Like