Zig Daily Report: Issues with cImport – Expected Type ‘T’, Found ‘T’ with Different Types

const c_one = @cImport({
    @cInclude("adwaita.h");
    @cInclude("gtk/gtk.h");
});

pub var layout_list: ?*c_one.GtkStringList = null;

const c_two = @cImport(@cInclude("gtk/gtk.h")); // could be in different file

pub fn main() void {
    layout_list = c_two.gtk_string_list_new(null); // could be in different file
}

Compilation Error:

main.zig:11:44: error: expected type '?*cimport.struct__GtkStringList', found '?*cimport.struct__GtkStringList'
    layout_list = c_two.gtk_string_list_new(null);
                  ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
main.zig:11:44: note: pointer type child 'cimport.struct__GtkStringList' cannot cast into pointer type child 'cimport.struct__GtkStringList'
.cache/zig/o/4eaa1196051a3c1d58c87fe72382fb3a/cimport.zig:48670:35: note: opaque declared here
pub const struct__GtkStringList = opaque {};
                                  ^~~~~~~~~
.cache/zig/o/81f98775d68e386e5e954b54f8336c55/cimport.zig:48670:35: note: opaque declared here
pub const struct__GtkStringList = opaque {};
                                  ^~~~~~~~~

The root of the problem lies in the two <span>@cImport</span> blocks where <span>GtkStringList</span> is treated as different opaque types, even though they should theoretically be the same.

Each time <span>@cImport</span> is used, it causes the file to be recompiled, creating new and different types. Therefore, it is recommended to have only one <span>@cImport</span> in your project. Please refer to the documentation here: ziglang.org/documentation/master#cImport.

https://github.com/ziglang/zig/issues/19420

Join Us

The Zig Chinese Community is an open organization dedicated to promoting the use of Zig within the Chinese community, and there are various ways to get involved:

  1. 1. Contribute and share[1] your experiences using Zig
  2. 2. Improve open source projects under the ZigCC organization[2]
  3. 3. Join WeChat groups[3] and Telegram groups[4]

Reference Links

<span>[1]</span>Contribute and share: https://ziglang.cc/contributing<span>[2]</span>Open source projects: https://ask.ziglang.cc/github<span>[3]</span>WeChat group: https://ask.ziglang.cc/weixin<span>[4]</span>Telegram group: http://ask.ziglang.cc/telegram

Leave a Comment