#Include C Code in Package Module

1 messages · Page 1 of 1 (latest)

eternal python
#

See next message

#

Example Zig Package for Including C Source from GitHub

eternal python
#

I am trying this:

    const expat = b.addModule("expat", .{
        .root_source_file = .{ .path = "src/root.zig" },
        .target = target,
        .optimize = optimize,
        .link_libc = true,
    });
    expat.addCSourceFile(.{
        .file = .{ .path = "libexpat/expat/lib/expat.h" },
        .flags = &.{"-std=c99"},
    });
#

but, it can't find the headers when I try to include them

#
const bindings = @cImport({
    @cInclude("expat.h");
});
eternal python
#

Include C Code in Package Module

#
C:\dev\expat-zig\zig-cache\o\c373578ee098d1b8f40047ead2cf5ee4\cimport.h:1:10: error: 'libexpat/expat/lib/expat.h' file not found
hollow acorn
#

Do not compile header files, here you just need expat.addIncludePath(.{ .path = "libexpat/expat/lib" }); on Zig master

eternal python
hollow acorn
#

bindings.member

eternal python
#

ah, thanks that works now. I had to add the include and libc to the test runner:

    const lib_unit_tests = b.addTest(.{
        .root_source_file = .{ .path = "src/root.zig" },
        .target = target,
        .optimize = optimize,
    });
    lib_unit_tests.linkLibC();
    lib_unit_tests.addIncludePath(.{
        .path = "libexpat/expat/lib",
    });

Is duplicating this code the correct solution?

hollow acorn
#

I'd expect that to not be necessary with a Zig compiler version from this week (should just need linkLibC + addIncludePath on the module) but for older Zig versions that looks correct.

eternal python