|
|
@ -42,29 +42,19 @@ const std = @import("std");
|
|
|
|
|
|
|
|
|
|
|
|
// new the import for C
|
|
|
|
// new the import for C
|
|
|
|
const c = @cImport({
|
|
|
|
const c = @cImport({
|
|
|
|
|
|
|
|
@cInclude("unistd.h");
|
|
|
|
// we use "standard input/output" from C
|
|
|
|
|
|
|
|
@cInclude("stdio.h");
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
pub fn main() void {
|
|
|
|
pub fn main() void {
|
|
|
|
|
|
|
|
|
|
|
|
// Due to a current limitation in the Zig compiler,
|
|
|
|
|
|
|
|
// we need a small workaround to make this exercise
|
|
|
|
|
|
|
|
// work on mac-os.
|
|
|
|
|
|
|
|
const builtin = @import("builtin");
|
|
|
|
|
|
|
|
const stderr = switch (builtin.target.os.tag) {
|
|
|
|
|
|
|
|
.macos => 1,
|
|
|
|
|
|
|
|
else => c.stderr,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// In order to output a text that can be evaluated by the
|
|
|
|
// In order to output a text that can be evaluated by the
|
|
|
|
// Zig Builder, we need to write it to the Error output.
|
|
|
|
// 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
|
|
|
|
// In Zig we do this with "std.debug.print" and in C we can
|
|
|
|
// specify the file to write to, i.e. "standard error (stderr)".
|
|
|
|
// specify the file descriptor i.e. 2 for error console.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Ups, something is wrong...
|
|
|
|
// In this case we use 'write' to output 17 chars,
|
|
|
|
const c_res = fprintf(stderr, "Hello C from Zig!");
|
|
|
|
// but something is missing...
|
|
|
|
|
|
|
|
const c_res = write(2, "Hello C from Zig!", 17);
|
|
|
|
|
|
|
|
|
|
|
|
// let's see what the result from C is:
|
|
|
|
// let's see what the result from C is:
|
|
|
|
std.debug.print(" - C result ist {d} chars\n", .{c_res});
|
|
|
|
std.debug.print(" - C result ist {d} chars\n", .{c_res});
|
|
|
|