Zig: Duplicate @cImport hangs Zig compiler

Created on 15 Jun 2020  路  2Comments  路  Source: ziglang/zig

The Zig compiler hangs when doing a double @cInclude of the same C header. This also persists when doing these @cIncludes across different Zig source files (which is how I encountered the bug in the first place). This behavior was only tested on Windows.

Most minimal reproducible example of this:

const first = @cImport({ @cInclude("time.h"); });
const second = @cImport({ @cInclude("time.h"); });

pub fn main() void {
    _ = first.time(null);
    _ = second.time(null);
}

Saved the code into main.zig and ran zig build-exe main.zig -lc. The compilation never seems to finish.

bug stage1

Most helpful comment

If one needs to use the same @cInclude in two separate .zig files, the workaround for now is to make the one include pub: pub const c_time = @cImport({ @cInclude("time.h"); });, and refer it in the second file: const c_time = @import("first_file.zig").c_time;.

All 2 comments

If one needs to use the same @cInclude in two separate .zig files, the workaround for now is to make the one include pub: pub const c_time = @cImport({ @cInclude("time.h"); });, and refer it in the second file: const c_time = @import("first_file.zig").c_time;.

This is a bug but note that duplicate import of the same C header file is almost certainly not what you want. @cImport docs

Was this page helpful?
0 / 5 - 0 ratings

Related issues

S0urc3C0de picture S0urc3C0de  路  3Comments

andrewrk picture andrewrk  路  3Comments

andrewrk picture andrewrk  路  3Comments

daurnimator picture daurnimator  路  3Comments

bheads picture bheads  路  3Comments