#Including a C library (SQLite) - error C import failed

1 messages · Page 1 of 1 (latest)

summer ice
#

I've been playing around with Zig for a few days and trying to use a C library (SQLite in this case) and right now I just fail to simply include it. The compiler always complains with error: C import failed which frankly speaking isn't very helpful because I can't even tell if the path for example is just wrong or something else. My code and directory tree look like this:

const std = @import("std");

const s = @cImport(@cInclude("c/sqlite3.h"));

pub fn main() !void {
    _ = s.sqlite3_libversion();

    // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
    std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
}

Anyone knows what I am doing wrong? I know there is zig-sqlite but that's not what I want to use right now and couldn't figure it out from its source code.

vernal cove
#

what's the output of zig translate-c c/sqlite3.h ?

autumn quiver
#

Are you linking to libc?

summer ice
#

I'm doing this:

summer ice
vernal cove
#

hmm weird

#

that's ok if u get that

#

but i wonder why u get error when building

#

btw u must build c/sqlite.c too

#

zig build-exe src/main.zig src/c/sqlite.c -lc

summer ice
vernal cove
#

try above command with -fstage1 flag

summer ice
#

Oh~ getting closer. It says it can't find c/sqlite3.h But src/c/sqlite3.h also doesn't work.

vernal cove
#

oh ok got it

#

zig build-exe src/main.zig src/c/sqlite.c -lc -Isrc/

summer ice
#

That worked~! Thanks a lot!