Skip to content

Instantly share code, notes, and snippets.

@tmlbl
Created October 19, 2023 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmlbl/d6b78581ab9bec29edf86663780f89e3 to your computer and use it in GitHub Desktop.
Save tmlbl/d6b78581ab9bec29edf86663780f89e3 to your computer and use it in GitHub Desktop.
Zig RocksDB test issue
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "csvd",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
exe.linkLibC();
exe.linkSystemLibraryName("rocksdb");
exe.addIncludePath(.{ .path = "/usr/include" });
exe.addModule("zinatra", b.createModule(.{
.source_file = .{ .path = "./zinatra/src/App.zig" },
}));
b.installArtifact(exe);
const tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = .Debug,
});
tests.linkLibC();
tests.linkSystemLibraryName("rocksdb");
tests.addIncludePath(.{ .path = "/usr/include" });
tests.addModule("zinatra", b.createModule(.{
.source_file = .{ .path = "./zinatra/src/App.zig" },
}));
b.installArtifact(tests);
const run_tests = b.addRunArtifact(tests);
const test_step = b.step("test", "Run the tests");
test_step.dependOn(&run_tests.step);
}
@tmlbl
Copy link
Author

tmlbl commented Oct 19, 2023

> zig test src/main.zig
src/DB.zig:2:13: error: C import failed
const rdb = @cImport(@cInclude("rocksdb/c.h"));
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/DB.zig:2:13: note: libc headers not available; compilation does not link against libc
referenced by:
    DB: src/DB.zig:21:10
    DB: src/DB.zig:20:16
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
/home/tim/tmlbl/ztsd/zig-cache/o/41ad5c3b1b49856f133a12651f3f2ba7/cimport.h:1:10: error: 'rocksdb/c.h' file not found
#include <rocksdb/c.h>
         ^

@tmlbl
Copy link
Author

tmlbl commented Oct 19, 2023

> zig build
> ./zig-out/bin/test
Test [1/1] test.data types... 
opening /tmp/rockstest-eraz5vck0e...
All 1 tests passed.

@tmlbl
Copy link
Author

tmlbl commented Oct 19, 2023

For anyone finding this, I had to pass linker flags to zig test. The needed command was:

zig test src/main.zig -lc -lrocksdb

The extra setup code for the test executable is not needed when running this way, so build.zig now looks like this:

const std = @import("std");

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

    const exe = b.addExecutable(.{
        .name = "csvd",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });

    exe.linkLibC();
    exe.linkSystemLibraryName("rocksdb");
    exe.addIncludePath(.{ .path = "/usr/include" });

    exe.addModule("zinatra", b.createModule(.{
        .source_file = .{ .path = "./zinatra/src/App.zig" },
    }));

    b.installArtifact(exe);

    const tests = b.addTest(.{
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = .Debug,
    });

    const run_tests = b.addRunArtifact(tests);

    const test_step = b.step("test", "Run the tests");
    test_step.dependOn(&run_tests.step);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment