Undefined symbol error when including mariadb c library

I’m getting this error when trying to use the mariadb client library:

error: undefined symbol: _mysql_get_client_version
note: referenced by /Users/jcattron/zig_sandbox/mariadb_project/.zig-cache/o/a845270bd562bf05ef2301f9ef3c083b/mariadb_project.o:_main.main

It is finding the mysql.h header file, but when I call c.mysql_get_client_version(); it seems to not be able to find the function in the library file.

Any ideas?

I’m using one of the latest 0.14 builds

Here’s the snippit from my build.zig:

    exe.addIncludePath(.{ .cwd_relative="/usr/local/opt/mariadb@11.4/include" });
    exe.addLibraryPath(.{ .cwd_relative="/usr/local/opt/mariadb@11.4/lib" });

Here’s my main.zig:

const std = @import("std");
const Allocator = std.mem.Allocator;
const c = @cImport({
    @cInclude("mysql/mysql.h");
});

const print = std.debug.print;

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer {
        switch (gpa.deinit()) {
            .ok => {},
            .leak => std.log.err("memory leak detected", .{}),
        }
    }

    const version = c.mysql_get_client_version();
    print("MySQL client version is {}\n", .{version});
}

Probably you will need to specify the library name at build.zig.

exe.linkSystemLibrary("mariadb");

And welcome to Ziggit !

1 Like

thank you @ktz_alias that is what I was missing

and thanks for the welcome, loving Zig!

1 Like

Just in case this helps anyone trying to wrap their head around how this all works, this is all it takes to use the mysql/maria c libraries:

in build.zig:

    exe.addIncludePath(.{ .cwd_relative="/usr/local/opt/mariadb@11.4/include/mysql" });
    exe.addLibraryPath(.{ .cwd_relative="/usr/local/opt/mariadb@11.4/lib" });
    exe.linkSystemLibrary("mariadb");

(your paths of course will be different depending on your OS and version of maria/mysql)

In your code:
const maria = @cImport({
    @cInclude("mysql.h");
});

From there, you can figure the rest out using this page as a starting point:
[MySQL - Zig cookbook]

If you’re trying to use a c library you built yourself and you have that c library as part of your project, you can do the following:

exe.addIncludePath(.{ .cwd_relative = "generate_include" }); // c source files
exe.addObjectFile(b.path("generate_lib/generate.a"));

Here I had built generate.a from some c code that built some files for a project awhile back, and wanted to use it in a zig program.

You can then use the library like this (I put the generate.h header in the generate_include directory in my project):

const generate = @cImport({
    @cInclude("generate.h");
});