"libc headers not available" when building test

I’m trying to run a test in my project with --test-filter but it fails with this error

src/root.zig:19:15: error: C import failed
pub const c = @cImport({
              ^~~~~~~~
src/root.zig:19:15: note: libc headers not available; compilation does not link against libc
referenced by:
    c: src/apu/buffer.zig:2:33
    apu.buffer.SampleBuffer: src/apu/buffer.zig:11:13
    14 reference(s) hidden; use '-freference-trace=16' to see all references
.zig-cache/o/ecb56e828e2cd32e919574aae0b45d08/cimport.h:1:10: error: 'SDL3/SDL.h' file not found
#include <SDL3/SDL.h>

I’ve tried using this code, but it didn’t work:

    const mod_tests = b.addTest(.{
        .root_module = mod,
    });
    const sdl_lib_tests = sdl_dep.artifact("SDL3_test");
    mod_tests.linkLibrary(sdl_lib_tests);

    // A run step that will run the test executable.
    const run_mod_tests = b.addRunArtifact(mod_tests);

    // Creates an executable that will run `test` blocks from the executable's
    // root module. Note that test executables only test one module at a time,
    // hence why we have to create two separate ones.
    const exe_tests = b.addTest(.{
        .root_module = exe.root_module,
    });
    exe_tests.linkLibrary(sdl_lib_tests);

    // A run step that will run the second test executable.
    const run_exe_tests = b.addRunArtifact(exe_tests);

I’ve already used .link_libc = true on the mod creation.

I’m using this for the SDL dependency. The full build.zig can be found here.

This implies you’re trying to run zig test directly instead of zig build test. If so, your build.zig is not being used at all.

Yes, I’m running zig test directly. Then, how can I work around this problem?

My suggestion would be to use zig build test and add test filter support as an option:

    const test_filters = b.option([]const []const u8, "test-filter", "Skip tests that do not match any filter") orelse &[0][]const u8{};

    const mod_tests = b.addTest(.{
        .root_module = mod,
        .filters = test_filters,
    });

    const exe_tests = b.addTest(.{
        .root_module = exe.root_module,
        .filters = test_filters,
    });

So then you’d do:

zig build test -Dtest-filter=your_filter

If you want to see what you’d need to do to run the same thing with zig test directly, you can pass --verbose:

zig build test -Dtest-filter=your_filter --verbose

which will print the commands run by the build system.

1 Like

That worked thanks! By the way, do you know how to configure launch.json in VSCode so that can debug individual tests? I think it uses the zig test command by default so it’s going to throw the same error.

For debugging tests as far as I know you can only debug the generated test executable, so specify it as any normal program. I assume filtering tests will prevent those tests from being included in the final executable and if not you can always set a breakpoint.

Make sure you’re compiling with LLVM for proper debugging support and if you’re using lldb use the pretty printer zig/tools/lldb_pretty_printers.py at master · ziglang/zig · GitHub to get proper array printing and other nice stuff.

I use zed for debugging and I think they use a different but similar format for debugging as vscode. here is a snippet I use, hope it helps.

  {
    "adapter": "CodeLLDB",
    "label": "example",
    "request": "launch",
    "name": "example",
    "program": "./zig-out/bin/example",
    "args": [],
    "stopOnEntry": false,
    "cwd": "${ZED_WORKTREE_ROOT}",
    "initCommands": ["command source ${ZED_WORKTREE_ROOT}/.lldbinit"]
  }

there is an lldb fork for the custom backend(s), but it requires building it from source, which isn’t bad, but it is slow.

I find that fork to be nicer to use, but not by a lot.

wiki for zig and lldb for more information about both.

1 Like

I’ve had to setup something like this in VSCode to filter the tests by name:

  • launch.json:
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug Executable",
            "args": [
                "./sample_roms/test.nes"
            ],
            "cwd": "${workspaceFolder}",
            "program": "./zig-out/bin/ness"
        },
        {
            "name": "Debug Filtered Test",
            "type": "lldb",
            "request": "launch",
            "program": "${workspaceFolder}/zig-out/tests/mod-test",
            "args": [],
            "cwd": "${workspaceFolder}",
            "preLaunchTask": "build-filtered-test",
        }
    ],
}
  • tasks.json:
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build-filtered-test",
      "type": "shell",
      "command": "zig",
      "args": [
        "build",
        "test",
        "-Dtest-filter=${input:testFilter}",
        "-Dno-run"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ],
      "inputs": [
        {
            "id": "testFilter",
            "type": "promptString",
            "description": "Enter test name",
            "default": ""
        }
    ]
}
2 Likes