2023-02-15 16:55:44 -05:00
|
|
|
//
|
|
|
|
// When Andrew Kelley announced the idea of a new programming language
|
|
|
|
// - namely Zig - in his blog on February 8, 2016, he also immediately
|
|
|
|
// stated his ambitious goal: to replace the C language!
|
|
|
|
//
|
|
|
|
// In order to be able to achieve this goal at all, Zig should be
|
|
|
|
// as compatible as possible with its "predecessor".
|
|
|
|
// Only if it is possible to exchange individual modules in existing
|
|
|
|
// C programs without having to use complicated wrappers,
|
|
|
|
// the undertaking has a chance of success.
|
|
|
|
//
|
|
|
|
// So it is not surprising that calling C functions and vice versa
|
|
|
|
// is extremely "smooth".
|
|
|
|
//
|
|
|
|
// To call C functions in Zig, you only need to specify the library
|
|
|
|
// that contains said function. For this purpose there is a built-in
|
|
|
|
// function corresponding to the well-known @import():
|
|
|
|
//
|
|
|
|
// @cImport()
|
|
|
|
//
|
|
|
|
// All required libraries can now be included in the usual Zig notation:
|
|
|
|
//
|
|
|
|
// const c = @cImport({
|
|
|
|
// @cInclude("stdio.h");
|
|
|
|
// @cInclude("...");
|
|
|
|
// });
|
|
|
|
//
|
|
|
|
// Now a function can be called via the (in this example) constant 'c':
|
|
|
|
//
|
|
|
|
// c.puts("Hello world!");
|
|
|
|
//
|
|
|
|
// By the way, most C functions have return values in the form of an
|
|
|
|
// integer value. Errors can then be evaluated (return < 0) or other
|
|
|
|
// information can be obtained. For example, 'puts' returns the number
|
|
|
|
// of characters output.
|
|
|
|
//
|
2023-03-14 08:43:50 -04:00
|
|
|
// So that all this does not remain a dry theory now, let's just start
|
2023-02-15 16:55:44 -05:00
|
|
|
// and call a C function out of Zig.
|
2023-02-16 13:28:10 -05:00
|
|
|
|
2023-02-15 16:55:44 -05:00
|
|
|
// our well-known "import" for Zig
|
|
|
|
const std = @import("std");
|
|
|
|
|
2024-01-13 09:27:58 -05:00
|
|
|
// and here the new import for C
|
2023-02-15 16:55:44 -05:00
|
|
|
const c = @cImport({
|
2023-02-16 12:33:06 -05:00
|
|
|
@cInclude("unistd.h");
|
2023-02-15 16:55:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
pub fn main() void {
|
|
|
|
|
2023-02-27 17:22:51 -05:00
|
|
|
// In order to output text that can be evaluated by the
|
2023-02-15 16:55:44 -05:00
|
|
|
// Zig Builder, we need to write it to the Error output.
|
2023-02-27 17:22:51 -05:00
|
|
|
// In Zig, we do this with "std.debug.print" and in C we can
|
2023-02-16 13:28:10 -05:00
|
|
|
// specify a file descriptor i.e. 2 for error console.
|
2023-02-15 16:55:44 -05:00
|
|
|
//
|
2023-02-27 17:22:51 -05:00
|
|
|
// In this exercise we use 'write' to output 17 chars,
|
2023-02-19 11:52:16 -05:00
|
|
|
// but something is still missing...
|
2024-06-19 12:19:46 -04:00
|
|
|
const c_res = c.write(2, "Hello C from Zig!", 17);
|
2023-02-15 16:55:44 -05:00
|
|
|
|
|
|
|
// let's see what the result from C is:
|
2023-02-27 17:22:51 -05:00
|
|
|
std.debug.print(" - C result is {d} chars written.\n", .{c_res});
|
2023-02-15 16:55:44 -05:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// Something must be considered when compiling with C functions.
|
|
|
|
// Namely that the Zig compiler knows that it should include
|
|
|
|
// corresponding libraries. For this purpose we call the compiler
|
|
|
|
// with the parameter "lc" for such a program,
|
|
|
|
// e.g. "zig run -lc hello_c.zig".
|
|
|
|
//
|