#including `libcheck` in zig build
1 messages · Page 1 of 1 (latest)
Are you using build.zig?
yep
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")
does that have to be relative? I am using devbox which uses the following ${DEVBOX_PACKAGES_DIR}/include pathname
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
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/
Zig is a new, independently developed low-level programming language. It’s a modern reimagining of C that attempts to retain C’s performance while embracing improvements from the last 30 years of tooling and language design.
Zig makes calling into C code easier than any other language I’ve used. Zig also treats unit testing as a firs...
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 ^^'
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
.path is deprecated now that we have b.path, youre meant to use cwd_relative for absolute paths
oh, passing -lcheck is probably going to break
Use unit_tests.linkSystemLibrary("check") instead
good to know
perfect, that did help
A few other notes:
.{ .path = "tests/fizzbuzz_test.c" }should benull-root_source_fileis for Zig sources, which you're not using in this case.{ .path = "src/fizzbuzz.c" }should beb.path("src/fizzbuzz.c")
(the .{ .path = ... } syntax is deprecated, as uncle says)
I am using devbox to install zig, so I am on version 0.11.0 is this still the case?
but also yes other than .path being deprecated
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
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?
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
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
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?
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
no I bumped to 0.12.0 a few moments ago
Oh yeah, weird, addCSourceFiles still takes strings rather than LazyPaths
interesting
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
.path stayed in 0.12 but deprecated, last week it was removed fully
although unrelated since that function doesnt take LazyPaths
no, i was talking about addCSourceFiles
but yeah, I misremembered it being changed
seems though addCSourceFiles has lost the abilty to include flags in 0.12, which seems to have been around in 0.11
mhmmm unsure why my build is complaining then
btw i think this is a deliberate design choice, let me see if i can find where andrew said that or if im making it up :p
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
Yeah it definitely is :)
Particularly with the new root field, which basically fixes the only thing you'd need LazyPath for