#@cImport failure when compiling notcurses

1 messages · Page 1 of 1 (latest)

old halo
#
run
└─ run demo
   └─ install
      └─ install demo
         └─ zig build-exe demo Debug native 3 errors
/home/user/Desktop/Projects/notcurses-zig-example/zig-cache/o/299cd342826228d3f19007bcf897aa76/cimport.zig:2884:58: error: use of undeclared identifier 'wcwidth'
        .width = @as(u8, @bitCast(@as(i8, @truncate(if ((wcwidth(@as(u32, @bitCast(@as(c_uint, c)))) < @as(c_int, 0)) or !(@as(u32, @bitCast(@as(c_uint, c))) != 0)) @as(c_int, 1) else wcwidth(@as(u32, @bitCast(@as(c_uint, c)))))))),
                                                         ^~~~~~~
/home/user/Desktop/Projects/notcurses-zig-example/zig-cache/o/299cd342826228d3f19007bcf897aa76/cimport.zig:3133:24: error: use of undeclared identifier 'wcswidth'
    var width: c_int = wcswidth(gclustarr, @as(c_int, 2147483647));
                       ^~~~~~~~
/home/user/Desktop/Projects/notcurses-zig-example/src/notcurses.zig:1:11: error: C import failed: AnalysisFail
const c = @cImport({
          ^~~~~~~~
referenced by:
    usingnamespace_0: /home/user/Desktop/Projects/notcurses-zig-example/src/notcurses.zig:4:20
error: the following command failed with 3 compilation errors:
/usr/bin/zig build-exe -freference-trace=256 -I/usr/include/qrcodegen -lqrcodegen -ODebug -Mroot=/home/user/Desktop/Projects/notcurses-zig-example/src/main.zig -lc --cache-dir /home/user/Desktop/Projects/notcurses-zig-example/zig-cache --global-cache-dir /home/user/.cache/zig --name demo --listen=-
#
Build Summary: 0/5 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run demo transitive failure
   ├─ zig build-exe demo Debug native 3 errors
   └─ install transitive failure
      └─ install demo transitive failure
         └─ zig build-exe demo Debug native (reused)
error: the following build command failed with exit code 1:
/home/user/Desktop/Projects/notcurses-zig-example/zig-cache/o/66ee968250f19a45f688d77162399557/build /usr/bin/zig /home/user/Desktop/Projects/notcurses-zig-example /home/user/Desktop/Projects/notcurses-zig-example/zig-cache /home/user/.cache/zig --seed 0xb8e7d9f3 -Zac610f84f9f0c477 run -freference-trace⏎
#

from what I could find this is caused by the following code in the include/notcurses/notcurses.h

static inline int
ncplane_putwstr_aligned(struct ncplane* n, int y, ncalign_e align,
                      const wchar_t* gclustarr) {
    int width = wcswidth(gclustarr, INT_MAX); // specifically this line
    int xpos = ncplane_halign(n, align, width);
    if(xpos < 0){
      xpos = 0;
    }
    return ncplane_putwstr_yx(n, y, xpos, gclustarr);
}
#

original c code compiles just fine, no compiler / linker errors

#

here is the build.zig

const Builder = @import("std").Build;

pub fn build(b: *Builder) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const notcurses_source_path = "deps/notcurses";

    const notcurses = b.addStaticLibrary(.{
        .name = "notcurses",
        .target = target,
        .optimize = optimize,
    });


    // notcurses.disable_sanitize_c = true;

    notcurses.linkLibC();
    notcurses.linkSystemLibrary("deflate");
    notcurses.linkSystemLibrary("ncurses");
    notcurses.linkSystemLibrary("readline");
    notcurses.linkSystemLibrary("unistring");
    notcurses.linkSystemLibrary("z");

    notcurses.addIncludePath(.{ .path = notcurses_source_path ++ "/include" });
    notcurses.addIncludePath(.{ .path = notcurses_source_path ++ "/build/include" });
    notcurses.addIncludePath(.{ .path = notcurses_source_path ++ "/src" });
    notcurses.addCSourceFiles(.{
        .files = &[_][]const u8{
            notcurses_source_path ++ "/src/compat/compat.c",
            :
            :
            notcurses_source_path ++ "/src/lib/windows.c",
        },
        .flags = &[_][]const u8{
            "-std=gnu11",
            "-D_GNU_SOURCE", // to make memory management work, see sys/mman.h
            "-DUSE_MULTIMEDIA=none",
            "-DUSE_QRCODEGEN=OFF",
            "-DPOLLRDHUP=0x2000",
        },
    });

    const exe = b.addExecutable(.{
        .name = "demo",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });
    b.installArtifact(exe);
    exe.linkLibC();

    exe.linkSystemLibrary("qrcodegen");

    const run_cmd = b.addRunArtifact(exe);
    run_cmd.step.dependOn(b.getInstallStep());
    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
}
dusky plover
#

at least i'm not alone 🥲

#

@old halo did you ever get this working?
I have a slightly more minimal example where I compile notcurses outside of zig, but am seeing the same issue. It's weird bc I think wcswidth comes from wchar.h which is being included in the notcurses header file

compact geode
dusky plover
#

like this?

const notcurses = @cImport({
    @cDefine("NOTCURSES_FFI", {});
    @cInclude("notcurses/notcurses.h");
});
#

put that in my main.zig, but no dice 😦

compact geode
#

yeah usually for header only libraries there is a flag that lets you exclude the implementation from the header
I thought NOTCURSES_FFI might have been a equivalent

dusky plover
#

notcurses isn't a header only library

compact geode
#

I know but it has implementations in the headers

#

and thats what is tripping up zig's translator

dusky plover
#

thereis a NOTCURSES_FFI ifdef in there

#

in the notcurses header

compact geode
#

there is but its not doing what I thought it might have been doing

dusky plover
#

ah

#

there's a cmake build option called BUILD_FFI_LIBRARY too, but it defaults to on

#

I've used stb and sokol with zig before and haven't run into anything like this either

compact geode
#

zig's c to zig translator does work with most c code but its not perfect

dusky plover
#

why is it getting tripped up here, though?

do you happen know of any open issue that could be related to this? If not, I should probably open one 😅

compact geode
#

there is an issue label for translate-c bugs on zig's github repo

dusky plover
#

I see a couple undeclared issues, but most seem pretty old and nothing about notcurses. I'll open one later day

#

Thank you, friendly pineapple

old halo
# dusky plover <@455007332603133963> did you ever get this working? I have a slightly more mini...

Hey, extremely late reply but yes, I did get it to work, you should see https://github.com/dundalek/notcurses-zig-example/, this works but the build.zig is outdated(0.11.0), updating that to 0.13.0 fixes it and works fine

GitHub

Demo showing how to use Notcurses library for building terminal UIs with Zig - dundalek/notcurses-zig-example

#

also I just remembered, there is a mis compilation in there, when the zig compiler internally translates the notcurses c code to zig, I had to manually patch the translated zig code emitted during compilation in the .zig-cache directory to make it work