#Embedding sqlite3 source code on Zig master branch

1 messages · Page 1 of 1 (latest)

cold dagger
#

Hi there!
I'm trying to embed the whole sqlite3 into my app so it compiles it statically and it's not required as a system dependency.

What I had was this:

    const sqlite_lib = b.addStaticLibrary(.{
        .name = "sqlite3",
        .target = target,
        .optimize = optimize,
        .link_libc = true,
    });

    const dep_sqlite = b.dependency("sqlite", .{});

    sqlite_lib.addCSourceFiles(.{
        .root = dep_sqlite.path(""),
        .files = &.{"sqlite3.c"},
    });

    sqlite_lib.installHeader(dep_sqlite.path("sqlite3.h"), "sqlite3.h");
    b.installArtifact(sqlite_lib);

    mod.linkLibrary(sqlite_lib);

Not that addStaticLibrary is removed, for what I've looked, I should use addLibrary, but it requires a root_module, that is not the app module, not sure what do I have to pass there

Thanks

umbral plume
#

you create a new module and add the C file to it

#

then set that as the library's root_module

cold dagger
#

Let me see if I can figure it out... Thanks! 😄

umbral plume
#

basically just move the stuff you used to put in addStaticLibrary into a new createModule call

cold dagger
#

I see, but on that module I left the root_source_fileemoty and then add the C source files? I though modules where only for Zig code

umbral plume
#

modules can have C code in them too

#

they've been able to for one or two versions now

#
const sqlite_mod = b.createModule(.{
    .target = target,
    .optimize = optimize,
    .link_libc = true,
});
const sqlite_dep = b.dependency("sqlite", .{});
sqlite_mod.addCSourceFile(.{
    .file = sqlite_dep.path("sqlite.c"),
});

const sqlite_lib = b.addLibrary(.{
    .name = "sqlite3",
    .root_module = sqlite_mod,
});
sqlite_lib.installHeader(sqlite_dep.path("sqlite3.h"), "sqlite3.h");

mod.linkLibrary(sqlite_lib);
#

Basically the same as your original, just with relevant stuff moved to the module instead of the lib

#

I also removed the installArtifact since I doubt you actually want to install libsqlite3.a into zig-out/lib

cold dagger
#

Is this info explained somewhere? I was looking into de docs, specially the build system one, but there seems to be almost none info/example on embedding a C project like this

umbral plume
#

what little docs there are tend to go out of date pretty quickly unfortunately ^^'

#

hmm maybe i'll update it when i have some time

cold dagger
#

Yeah, a couple of days ago the Io rewrite broke, I'km still figuring out how to migrate the stdout printing the code

#

And yesterday the building code

#

Just tested your solution, working perfectly. Really thanks a lot 😄

cold dagger