I am doing a side project and maybe this may be not a good first step, but
I am looking to include a C library to interact with a USB device in order
to build an easy-to-use cross-platform zig library.
So, I am considering to switch for this project from Rust to zib because I think
for this kind of job, zig is a good alternative to rust, but I find difficult.
BTW my current architecture is hosted on github at https://github.com/vincenzopalazzo/unified-hwi and looks like the following
-external
- hidapi
- libusb
- src
- main.zig
- hwi
- device.zig
- build.zig
My build.zig looks like
const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) 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 optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
const lib = b.addStaticLibrary(.{
.name = "unified-hwi",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
lib.linkLibC();
lib.addIncludePath(std.Build.LazyPath.relative("external/hidapi/libusb"));
lib.addIncludePath(std.Build.LazyPath.relative("external/hidapi/hidapi"));
lib.addIncludePath(std.Build.LazyPath.relative("external/libusb/libusb"));
lib.addLibraryPath(std.Build.LazyPath.relative("external/hidapi/hidapi"));
lib.addCSourceFile(.{ .file = std.Build.LazyPath.relative("./external/hidapi/libusb/hid.c"), .flags = &[_][]const u8{"-std=c99"} });
// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
b.installArtifact(lib);
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const main_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
main_tests.linkLibC();
main_tests.addIncludePath(std.Build.LazyPath.relative("external/hidapi/libusb"));
main_tests.addIncludePath(std.Build.LazyPath.relative("external/hidapi/hidapi"));
main_tests.addIncludePath(std.Build.LazyPath.relative("external/libusb/libusb"));
main_tests.addLibraryPath(std.Build.LazyPath.relative("external/hidapi/hidapi"));
main_tests.addCSourceFile(.{ .file = std.Build.LazyPath.relative("./external/hidapi/libusb/hid.c"), .flags = &[_][]const u8{"-std=c99"} });
const run_main_tests = b.addRunArtifact(main_tests);
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build test`
// This will evaluate the `test` step rather than the default, which is "install".
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&run_main_tests.step);
}
When I will go to build the library
➜ unified-hwi git:(macros/api) nix develop
[vincent@vincent-arch unified-hwi]$ make
zig fmt .
zig build --verbose-cimport
zig test src/main.zig
src/hw/cold_card.zig:4:13: error: C import failed
const hid = @cImport({
^~~~~~~~
src/hw/cold_card.zig:4:13: note: libc headers not available; compilation does not link against libc
referenced by:
ColdCardUSB: src/hw/cold_card.zig:32:14
ColdCardUSB: src/hw/cold_card.zig:25:21
remaining reference traces hidden; use '-freference-trace' to see all reference traces
/home/vincent/Github/unified-hwi/zig-cache/o/d7b010b333c576d588250325510d6e84/cimport.h:2:10: error: 'hid.h' file not found
#include <hid.h>
^
error: Recipe `default` failed on line 3 with exit code 1
[vincent@vincent-arch unified-hwi]$
I am not able to understand what kind of path cInclude
want, and why in this case is not able to find it?
I think I have done all what the guide tell to include hidapi/BUILD.md at master · libusb/hidapi · GitHub
What I am missing?