#how can i import libnotify correctly?

1 messages · Page 1 of 1 (latest)

last citrus
#

hi foks, i'm trying to use libnotify, the c headers just lays in /usr/include,
but failed to make this code compiled with zig 0.10.0-dev.4333+f5f28e0d2, what am doing wrong?

const std = @import("std");
const print = std.debug.print;

const C = @cImport({

    // /usr/include/glib-2.0/glib.h
    @cInclude("glib.h");
    @cInclude("glib-unix.h");

    // /usr/include/libnotify/notify.h
    @cInclude("libnotify/notify.h");
});

pub fn main() !void {
    print("{any}\n", .{C});
}
> zig run -lc -lnotify -lglib-2.0 -I/usr/include notify.zig
notify.zig:4:11: error: C import failed
const C = @cImport({
          ^~~~~~~~
referenced by:
    main: notify.zig:15:24
    comptime_0: /opt/zig-linux-x86_64-0.10.0-dev.4333+f5f28e0d2/lib/std/start.zig:60:50
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
near oyster
#

i very much recommend using a build.zig as the below is all that needs to be added, linkSystemLibrary uses pkgconfig under the hood to know the dependant libraries and include paths

exe.linkLibC();
exe.linkSystemLibrary("libnotify");

if you want to use zig run/build-exe/build-lib directly the command will be (assuming you have all the deps and the paths are correct:

zig run notify.zig -lc -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/sysprof-4 -I/usr/include/libpng16 -I/usr/include/libmount -I/usr/include/blkid -lnotify -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0
#

although thinking about it you could use pkgconfig directly like you would with c:

zig run notify.zig -lc `pkg-config --libs --cflags libnotify`
last citrus
#

wow, thank you! i have this set in build.zig too, but i even dont know it works

#

i never knew this pkg-config command, great tips!