Call librdkafka result in Segmentation fault

main.zig

const std = @import("std");

const c = @cImport({
    @cInclude("librdkafka/rdkafka.h");
});

pub fn main() !void {
    const conf = c.rd_kafka_conf_new();  //seg fault here
    defer c.rd_kafka_conf_destroy(conf);

    if (conf != null) {
        std.debug.print("conf is ok\n", .{});
    } else {
        std.debug.print("Failed to get conf\n", .{});
    }
}

build.zig

const std = @import("std");

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

    b.installArtifact(exe);
}

run program print out:

Segmentation fault at address 0x0
???:?:?: 0x0 in ??? (???)
Aborted (core dumped)

system:

Distributor ID: Ubuntu
Description:    Ubuntu 20.04.6 LTS
Release:        20.04
Codename:       focal

ii  librdkafka-dev:amd64                     1.2.1-1ubuntu1            amd64                     library implementing the Apache Kafka protocol (development headers)

any suggestion?

Try linking to libc as well.

4 Likes

exe.linkLibC();
This did work :laughing:.

2 Likes