From 4cf3bd63a22bd2f88c1f04cddac31aa19bc78a9d Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Wed, 15 Feb 2023 22:55:44 +0100 Subject: added the first C exercise --- build.zig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'build.zig') diff --git a/build.zig b/build.zig index 930fc56..32ee69c 100644 --- a/build.zig +++ b/build.zig @@ -31,6 +31,10 @@ const Exercise = struct { /// We need to keep track of this, so we compile without the self hosted compiler @"async": bool = false, + /// This exercise makes use of C functions + /// We need to keep track of this, so we compile with libc + C: bool = false, + /// Returns the name of the main file with .zig stripped. pub fn baseName(self: Exercise) []const u8 { assert(std.mem.endsWith(u8, self.main_file, ".zig")); @@ -461,6 +465,11 @@ const exercises = [_]Exercise{ // .output = "ABCDEF", // .@"async" = true, // }, + .{ + .main_file = "093_hello_c.zig", + .output = "Hello C from Zig! - C result ist 17 chars", + .C = true, + }, .{ .main_file = "999_the_end.zig", .output = "\nThis is the end for now!\nWe hope you had fun and were able to learn a lot, so visit us again when the next exercises are available.", @@ -725,6 +734,11 @@ const ZiglingStep = struct { // zig_args.append("-fstage1") catch unreachable; // } + // Enable C support for exercises that use C functions + if (self.exercise.C) { + zig_args.append("-lc") catch unreachable; + } + if (builder.color != .auto) { zig_args.append("--color") catch unreachable; zig_args.append(@tagName(builder.color)) catch unreachable; -- cgit v1.2.3 From dc187889c1a0cec8cb0e7de67df96ebc9e981995 Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Thu, 16 Feb 2023 19:28:10 +0100 Subject: some improvements in the description --- build.zig | 8 ++++---- exercises/093_hello_c.zig | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'build.zig') diff --git a/build.zig b/build.zig index c23e92e..1abe5c7 100644 --- a/build.zig +++ b/build.zig @@ -465,14 +465,14 @@ const exercises = [_]Exercise{ // .output = "ABCDEF", // .@"async" = true, // }, + .{ + .main_file = "092_interfaces.zig", + .output = "Daily Insect Report:\nAnt is alive.\nBee visited 17 flowers.\nGrasshopper hopped 32 meters.", + }, .{ .main_file = "093_hello_c.zig", .output = "Hello C from Zig! - C result ist 17 chars", .C = true, -}, -.{ - .main_file = "092_interfaces.zig", - .output = "Daily Insect Report:\nAnt is alive.\nBee visited 17 flowers.\nGrasshopper hopped 32 meters.", }, .{ .main_file = "999_the_end.zig", diff --git a/exercises/093_hello_c.zig b/exercises/093_hello_c.zig index 1877e04..b294f38 100644 --- a/exercises/093_hello_c.zig +++ b/exercises/093_hello_c.zig @@ -36,11 +36,11 @@ // // So that all this does not remain a dry theroy now, let's just start // and call a C function out of Zig. -// + // our well-known "import" for Zig const std = @import("std"); -// new the import for C +// and here the new the import for C const c = @cImport({ @cInclude("unistd.h"); }); @@ -50,9 +50,9 @@ pub fn main() void { // In order to output a text that can be evaluated by the // Zig Builder, we need to write it to the Error output. // In Zig we do this with "std.debug.print" and in C we can - // specify the file descriptor i.e. 2 for error console. + // specify a file descriptor i.e. 2 for error console. // - // In this case we use 'write' to output 17 chars, + // In this exercise we use 'write' to output 17 chars, // but something is missing... const c_res = write(2, "Hello C from Zig!", 17); -- cgit v1.2.3