ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.09k stars 2.49k forks source link

cimport, error: dependency loop detected #18247

Open jimying opened 9 months ago

jimying commented 9 months ago

Zig Version

0.12.0-dev.1808+69195d0cd

Steps to Reproduce and Observed Behavior

I want to call pjsip(open source: https://github.com/pjsip/pjproject) library in zig.

Here my simple sample : https://github.com/jimying/testpj.zig

steps to build sample (tested in linux x86_64):

# 1. install pjsip library
sh install_pjsip.sh

# 2. build, this step is fail, `error: dependency loop detected`.
zig build 

Expected Behavior

build and run ok

jimying commented 9 months ago

I build success by modify the declare sequeue and works fine.

At the beginning, I suspected that it's a problem with translate-c. But i compare the cimport.zig files, it's same! This is a very special problem, hope this sample can help to debug.

Follow is the modify patch:

     // problem1: test memory pool  (error: dependency loop detected)
+    var pool: [*c]c.pj_pool_t = undefined;
     var cp: c.pj_caching_pool = undefined;
     c.pj_caching_pool_init(&cp, null, 0);

-    const pool = c.pj_pool_create(&cp.factory, "test", 1000, 1000, null);
+    pool = c.pj_pool_create(&cp.factory, "test", 1000, 1000, null);
     _ = c.pj_pool_alloc(pool, 100);
     c.pj_pool_release(pool);
byte911 commented 5 months ago
const uv = @cImport({
    @cInclude("uv.h");
});

pub fn main() void {
    const server: uv.uv_tcp_t = undefined;
    _ = server;
}
const std = @import("std");

pub fn build(b: *std.Build) void {
    const exe = b.addExecutable(.{
        .name = "uv",
        .root_source_file = .{ .path = "main.zig" },
        .target = b.host,
    });
    exe.linkSystemLibrary("libuv");

    b.installArtifact(exe);
}
runner@MacBook-Pro libuv % zig build --summary all
install
└─ install uv
   └─ zig build-exe uv Debug native 1 errors
/Users/runner/libuv/zig-cache/o/2df95e3579cfa20689bca1733cc0bcce/cimport.zig:7233:5: error: dependency loop detected
pub const uv_read_cb = ?*const fn ([*c]uv_stream_t, isize, [*c]const uv_buf_t) callconv(.C) void;
~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: the following command failed with 1 compilation errors:
/Users/runner/.asdf/installs/zig/0.12.0/zig build-exe -I/opt/homebrew/Cellar/libuv/1.47.0/include -L/opt/homebrew/Cellar/libuv/1.47.0/lib -luv -ODebug -Mroot=/Users/runner/libuv/t.zig --cache-dir /Users/runner/libuv/zig-cache --global-cache-dir /Users/runner/.cache/zig --name uv --listen=-
Build Summary: 0/3 steps succeeded; 1 failed
install transitive failure
└─ install uv transitive failure
   └─ zig build-exe uv Debug native 1 errors
error: the following build command failed with exit code 1:
/Users/runner/libuv/zig-cache/o/522c69f7cc4b303ce635a4ba0daa6d56/build /Users/runner/.asdf/installs/zig/0.12.0/zig /Users/runner/libuv /Users/runner/libuv/zig-cache /Users/runner/.cache/zig --seed 0xaa3c6927 -Zb77af56ad238101e --summary all
runner@MacBook-Pro libuv %

thanks.

sphaerophoria commented 4 months ago

Hit this while trying to use miniaudio. Found a stripped down repro which might be helpful

test.h

struct A;
typedef void (*function_callback)(struct A*);

struct A {
    function_callback callback;
};

struct B {
    function_callback callback;
};

. test.zig

const c = @cImport({
    @cInclude("test.h");
});

pub fn main() void {
    const v: c.B = undefined;
    _ = v;
}

Run with zig run -I. test.zig

Observed on zig 0.12.0 and nightly zig-linux-x86_64-0.13.0-dev.75+5c9eb4081