I’m trying to learn zig and I wanted to test a single function for learning purposes even though it’s not really required I wanted to know that its doing what I expected
const std = @import("std");
const c = @cImport(@cInclude("include/miniaudio.h"));
const Playlist = struct {
songs: std.ArrayList([]const u8), // Dynamic array for song file paths
current_index: usize, // Current song index
paused: bool, // Flag to check if the song is paused
context: c.ma_context, // Miniaudio context
device: c.ma_device, // Miniaudio device for audio playback
sound: c.ma_sound, // Miniaudio sound
};
// Initialise the playlist with default values
fn initPlaylist() Playlist {
return Playlist{
.song = std.ArrayList([]const u8).init(std.heap.page_allocator),
.current_index = 0,
.paused = false,
.context = undefined,
.device = undefined,
.sound = undefined,
};
}
test "test initPlaylist" {
var playlist = initPlaylist();
const expect = std.testing.expect;
// Check that the songs array is initialised and empty
try expect(playlist.songs.items.len == 0);
// Check the current index is initialised and 0
try expect(playlist.current_index == 0);
// Check the paused flag is initialised and false
try expect(playlist.paused == false);
// Check that the context is initialized to undefined
try expect(playlist.context == undefined);
// Check that the device is initialized to undefined
try expect(playlist.device == undefined);
// Check that the sound is initialized to undefined
try expect(playlist.sound == undefined);
// Clean up to prevent a memory leak
playlist.songs.deinit();
}
Note I’ve tried the following
const c = @cImport({
@cInclude("include/miniaudio.h");
});
And
const c = @cImport({
@cInclude("miniaudio.h");
});
I keep getting the same error
Build.zig:
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("zig-music", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();
exe.addIncludePath("include");
// exe.linkSystemLibrary("c");
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const exe_tests = b.addTest("src/main.zig");
exe_tests.setTarget(target);
exe_tests.setBuildMode(mode);
exe_tests.addIncludePath("include");
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step);
}
Project:
tree .
.
├── build.zig
├── include
│ └── miniaudio.h
├── src
│ └── main.zig
Error when running zig build test
:
zig build test
/tmp/zig-music/src/main.zig:2:11: error: C import failed
const c = @cImport(@cInclude("miniaudio.h"));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/zig-music/src/main.zig:2:11: note: libc headers not available; compilation does not link against libc
referenced by:
Playlist: /tmp/zig-music/src/main.zig:8:14
Playlist: /tmp/zig-music/src/main.zig:4:18
remaining reference traces hidden; use '-freference-trace' to see all reference traces
error: test...
error: The following command exited with error code 1:
/usr/bin/zig test /tmp/zig-music/src/main.zig --cache-dir /tmp/zig-music/zig-cache --global-cache-dir ~/.cache/zig --name test -I /tmp/zig-music/include --enable-cache
error: the following build command failed with exit code 1:
/tmp/zig-music/zig-cache/o/b88a9267b8f4ddb25693e550dad2dcfb/build /usr/bin/zig /tmp/zig-music /tmp/zig-music/zig-cache ~/.cache/zig test
I’m using Void Linux
zig env
{
"zig_exe": "/usr/bin/zig",
"lib_dir": "/usr/lib/zig",
"std_dir": "/usr/lib/zig/std",
"global_cache_dir": "~/.cache/zig",
"version": "0.10.1",
"target": "x86_64-linux.6.6.31...6.6.31-gnu.2.39"
After actually looking at the output of zig env I’m wondering is this because of the version of zig installed?
I am happy to grab the latest version and try again, but that isn’t going to help my understanding of why this error is happening.
Thank you.