#including `libcheck` in zig build

1 messages · Page 1 of 1 (latest)

proven jay
#

unsure if anyone can help, I have just started to see if its possible to use Zig to build my C executables and seems as though I've done ok. I now want to be able to run tests (first using an external c lib like check and then try use zig) I need to be able to have the inlcude path but don't know how. any suggestions thanks

uncut crystal
#

Are you using build.zig?

proven jay
#

yep

uncut crystal
#

okie cool

#

step.addIncludePath will add an include dir

#

It takes a LazyPath. you can construct one for a relative path from the build root using b.path("path/to/include")

proven jay
#

does that have to be relative? I am using devbox which uses the following ${DEVBOX_PACKAGES_DIR}/include pathname

uncut crystal
#

for an absolute path, you should probably use .{ .path = "/path/to/include" }? Not sure, it's kind of an unusual case

#

Since absolute paths are extremely non-portable, so you should avoid them if possible

proven jay
#

ah seems I've a new error (cannot find symbol, likely out the scope of a Zig community though). maybe I should go over one of the links I found in the server history and use Zig's native zig build test instead https://mtlynch.io/notes/zig-unit-test-c/

uncut crystal
#

feel free to send the error and I'll see if I can help :)

#

Or just write your tests in zig if you prefer, might be easier than wrestling with build issues haha ^^'

proven jay
#

yes, plus I wanna learn zig anyway so could be fine to have my tests in Zig even if I am not writing Zig in source.

But for more clarity my build looks like:

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

    // const unit_tests = b.addTest(.{
    const unit_tests = b.addExecutable(.{
        .name = "fizzbuzz_test",
        .root_source_file = .{ .path = "tests/fizzbuzz_test.c" },
        .target = target,
        .optimize = optimize,
    });

    unit_tests.linkLibC();
    unit_tests.addIncludePath(.{ .path = "/Users/xxx/.local/share/devbox/global/default/.devbox/nix/profile/default/include" });
    unit_tests.addIncludePath(.{ .path = "src" });
    unit_tests.addCSourceFiles(&.{
        "src/fizzbuzz.c",
    }, &.{"-lcheck"});

    b.installArtifact(unit_tests);

    // const run_unit_tests = b.addRunArtifact(unit_tests);
    // const test_step = b.step("test", "Run unit tests");

    // test_step.dependOn(&run_unit_tests.step);
}

with a filetree looking like:

src/fizzbuzz.c
src/fizzbuzz.h
tests/fizzbuzz_test.c
obtuse saffron
uncut crystal
#

oh, passing -lcheck is probably going to break

#

Use unit_tests.linkSystemLibrary("check") instead

proven jay
#

perfect, that did help

uncut crystal
#

(the .{ .path = ... } syntax is deprecated, as uncle says)

proven jay
uncut crystal
#

Oh, 0.11, right

#

I'd recommend updating to 0.12

obtuse saffron
#

but also yes other than .path being deprecated

uncut crystal
#

It makes a number of improvements to the build system, and it'll be easier to get help if you're on the latest version

proven jay
#

ah just searched an 0.12 is there will update that now thanks.

#

finally (though this query likely all good) if I want to use zig build test that cannot be done without a zig file correct?

uncut crystal
#

zig build test isn't a built in thing, you can just define a test step and point it at whatever you want

#

But to use the zig test framework (ie. zig test, or b.addTestStep), you need to use zig yes

proven jay
#

ah thanks, makes sense. also updating to 0.12 worked though I may try to learn Zig through some tests and gradually into some src code as i learn. appreciatie you both @uncut crystal @obtuse saffron

proven jay
#

for some reason, this works (the LSP doesn't seem to show any issues)

const std = @import("std");

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

    const unit_tests = b.addExecutable(.{
        .target = target,
        .optimize = optimize,
        .name = "fizzbuzz_test",
    });
    unit_tests.linkLibC();
    unit_tests.addCSourceFiles(.{
        .files = &.{ "tests/fizzbuzz_test.c", "src/fizzbuzz.c" },
    });

    unit_tests.linkSystemLibrary("check");

    b.installArtifact(unit_tests);
}

I didn't need to use b.path nor does it seem I needed to include the fizzbuzz.h via the unit_test.addIncludePath is that just because of how novel my project code is?

uncut crystal
#

you can include files in the same directory without needing to add include paths

#

You should need b.path though, unless you're still on 0.11?

#

fwiw, LSP will not necessarily show you compile errors

proven jay
#

no I bumped to 0.12.0 a few moments ago

uncut crystal
#

Oh yeah, weird, addCSourceFiles still takes strings rather than LazyPaths

#

interesting

proven jay
#

ahh ok, well will see in issues, if there was. reason. the object files still being created and the resulting executable still running. Ill try an example where the header is in a separate directory

#

but for the sort of projects I do, I dont do that much anyway

obtuse saffron
#

although unrelated since that function doesnt take LazyPaths

uncut crystal
#

no, i was talking about addCSourceFiles

#

but yeah, I misremembered it being changed

proven jay
#

seems though addCSourceFiles has lost the abilty to include flags in 0.12, which seems to have been around in 0.11

proven jay
#

mhmmm unsure why my build is complaining then

obtuse saffron
proven jay
#
 error: member function expected 1 argument(s), found 2
    tests.addCSourceFiles(.{
    ~~~~~^~~~~~~~~~~~~~~~
/nix/store/vi8yzi9z4h5yd50x5acbqmzvmycbj0rk-zig-0.12.0/lib/zig/std/Build/Step/Compile.zig:784:5: note: function declared here
pub fn addCSourceFiles(self: *Compile, options: Module.AddCSourceFilesOptions) void {
#

wait, I think I may know why

proven jay
#

nvm I was being silly

#

thanks again

uncut crystal
#

Particularly with the new root field, which basically fixes the only thing you'd need LazyPath for