#'C import failed' but only when running zig test

1 messages · Page 1 of 1 (latest)

spiral lichen
#

zig build test is returning the following error on 0.14.0-dev. When using zig build run, no errors appear and str_copy produces the correct output.

src/main.zig:2:14: error: C import failed
const libc = @cImport({
             ^~~~~~~~
src/main.zig:2:14: note: libc headers not available; compilation does not link against libc
error: 'string.h' file not found
#include <string.h>
const std = @import("std");
const c = @cImport({
    @cInclude("string.h");
});

fn str_copy(str: [:0]const u8) ![*:0]u8 {
    return c.strdup(str) orelse error.OutOfMemory;
}

test "should create c-style string" {
    const str = try str_copy("foo");
    try std.testing.expect(str != null);
}

pub fn main() !void {
    _ = try str_copy("foo");
}
white perch
#

you need to link libc in the test step, not just the compile step