raz / site / content / tour / 14-c.txt

92 lines · 2.8 KiB · raw · history · last change a5145f64

1title: Talking to C
2part: Go
3summary: Call a C library, be called back, share variables, and mind the two buffers.
4---
5```raz Qsort run
6```
7
8An `extern` procedure is found by its name in the C library, or in a library a `link` line names. Its parameters are integers, reals, pointers and procedures, as C declares them; a raz procedure of such types can be given to C to call back, as `order` is. A record goes to C by pointer, never by value.
9
10## What using C costs
11
12A program that uses C is linked against it dynamically and starts through it, so that C's own set-up and clean-up run; one that doesn't is a static executable with nothing in it but its own code. A program uses C if any module it uses has an `extern` or a `link` of a library.
13
14## Varargs
15
16`...` after an `extern`'s parameters takes any number of further arguments, as `printf` wants. A string literal goes as a C string, an integer constant as an `INT`, a real constant as an `F64`. An `F32` is an error there, since C promotes it; write `F64(x)`.
17
18```raz Say run
19module Say;
20import Out;
21import SYSTEM;
22
23extern proc printf(format: ^U8; ...): I32;
24extern proc fflush(f: ^U8): I32;
25extern proc puts(s: ^U8): I32;
26
27proc main*;
28var n: INT; x: F32;
29begin
30 n := 42;
31 x := 2.5;
32 Out.str("raz first\n");
33 Out.flush; (* Out has a buffer; so does C *)
34 _ := printf("C says %ld and %.2f and %s\n", n, F64(x), "hello");
35 _ := puts("and puts");
36 _ := fflush(nil); (* nil: every C stream *)
37 Out.str("raz again\n");
38end;
39
40end Say.
41```
42
43`Out` and C's `printf` keep two buffers, so a program that writes with both flushes one before it writes with the other, or the text comes out of order.
44
45## Libraries, and C's variables
46
47`link "libm.so.6";` names a shared library to load at run time. An `extern var` is C's variable itself: reading it reads C's, assigning it writes C's.
48
49```raz Env run
50module Env;
51import Out;
52import SYSTEM;
53
54extern var environ: ^^U8;
55extern proc strlen(s: ^U8): UINT;
56extern proc getenv(name: ^U8): ^U8;
57
58proc main*;
59var p: ^U8; n: INT;
60begin
61 n := 0;
62 while environ[n] # nil do INC(n); end;
63 if n > 0 then Out.str("the environment is not empty\n"); end;
64 p := getenv("RAZ_NO_SUCH_VARIABLE");
65 if p = nil then Out.str("not set\n"); end;
66 Out.int(INT(strlen("six"))); Out.ln;
67end;
68
69end Env.
70```
71
72## Callbacks
73
74Every raz procedure uses the System V calling convention, so any with parameters and a result of C's types may be given to C to call, as `qsort` is given `order` above. The procedure may not be a closure: it has only its arguments, so state travels through the pointer C hands back or a module variable.
75
76```raz Roots run
77module Roots;
78import Fmt;
79import SYSTEM;
80
81link "libm.so.6";
82
83extern proc cbrt(x: F64): F64;
84extern proc hypot(x, y: F64): F64;
85
86proc main*;
87begin
88 Fmt.print("{} {}\n", [Fmt.real(cbrt(27.0), 3), Fmt.real(hypot(3.0, 4.0), 3)]);
89end;
90
91end Roots.
92```