#Converting C to Zig for Beginners

1 messages · Page 1 of 1 (latest)

ocean plover
#

Converting a trivial C project into Zig, one file at a time. It's aimed at true beginners. I hope it's useful for someone who wants to try out Zig's interop with C, especially building a C project with Zig. Please send me feedback, I'd love to make the post better!

https://lukewiebe.com/blog/c-to-zig/

fallow grail
#

zig translate-c

languid canopy
#

translate-c is only designed for headers

fallow grail
#

Well it works for normal stuff but I get your point

ocean plover
#

Could you elaborate @fallow grail? I'd love to know where I could use zig translate-c in this post.

fallow grail
#

I havnt read the blog, prob should but you can use the addTranslateC function in build that will idk translate c code

#
const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{
        .name = "main",
        .root_module = b.addTranslateC(.{
            .root_source_file = b.path("main.c"),
            .target = target,
            .optimize = optimize,
            .link_libc = true,
        }).createModule(),
    });

    b.installArtifact(exe);

    const run = b.addRunArtifact(exe);
    const run_step = b.step("run", "Runs the app");
    run_step.dependOn(&run.step);
}

This works

#

Tho I see why you did things like you did since its how it was before and since addTranslateC wasnt used much before

#

also yes this works with multiple C files, just give it the entry and it works

#

also don't use @cImport since its depricated

#

also the print example is good for the writer but I think a more valid translation is just
std.debug.print rather than using the writer

#

also I've never used b.graph.host I dont recommend using it eighter since you don't get power over the target via the command line

#

also compiling every c source file into its own object is quite a bad way of doing it

#

Well I wont roast your blog anymore, its not always easy knowing what to do especialy since zig's build system is changing a lot currently
idk I hope this helped you in some way and feel free to ask about anything more

#

@ocean plover

#

Also I'd recommend you share your blog here also https://ziggit.dev/

ocean plover
#

No not at all, this is great. Thanks!

#

I'm looking for feedback since it makes the resource more useful and I get to learn too, I'll definitely change the writer and add the standard target stuff to the build.zig

#

Thanks so much!

fallow grail
#

np have a good rest of your day!👌