Skip to content

Instantly share code, notes, and snippets.

@mischief
Last active December 11, 2021 19:20
Show Gist options
  • Save mischief/0d771bd33ca34255e4dced5fc4e93ace to your computer and use it in GitHub Desktop.
Save mischief/0d771bd33ca34255e4dced5fc4e93ace to your computer and use it in GitHub Desktop.
zig linkage failure 0.9.0-dev.1936+77836e08a
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("call-c", "main.zig");
exe.linkLibC();
exe.linkSystemLibrary("libsystemd");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();
const run_cmd = exe.run();
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);
const exe_tests = b.addTest("main.zig");
exe_tests.setBuildMode(mode);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step);
}
const std = @import("std");
const c = @cImport(@cInclude("systemd/sd-bus.h"));
pub fn main() anyerror!void {
var bus: ?*c.sd_bus = null;
var r = c.sd_bus_default_system(&bus);
if (r <= 0) {
std.log.info("dbus failed: {}", .{r});
return;
}
defer c.sd_bus_unrefp(&bus);
var msg: ?*c.sd_bus_message = null;
var err = c.sd_bus_error{ .name = null, .message = null, ._need_free = 0 };
r = c.sd_bus_get_property(bus, "org.freedesktop.timedate1", "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "TimeUSec", &err, &msg, "t");
if (r < 0) {
std.log.info("SD bus error: {s} - {s}", .{ err.name, err.message });
return;
}
defer c.sd_bus_message_unrefp(&msg);
if (msg) |m| {
var ts: c_ulonglong = 0;
_ = c.sd_bus_message_read_basic(m, 't', &ts);
std.log.info("The time is: {}", .{ts});
}
}
test "basic test" {
try std.testing.expectEqual(10, 3 + 7);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment