.\" Made by website/plain/build.mjs from website/words/extern.md; change that file. .TH SG-EXTERN 7 2026-11-28 "SG" "SG reference" .SH NAME extern \- declares the functions of a C library, or of a C file beside the program, to call inside unsafe .SH SYNOPSIS .TP \fBextern { ... }\fR functions of the C library itself .TP \fBextern "m" { ... }\fR functions of a library, linked as -lm .TP \fBextern "z", "zlib" { ... }\fR the names one library has on different systems; the first one found is linked .TP \fBextern "./native.c" { ... }\fR functions of a C file in the module's folder, compiled with the program .TP \fBR name(T a, U b)\fR one function on each line, with no body .TP \fBR name(T a, ...)\fR a function that takes a varying count of arguments after the named ones .TP \fBstruct FILE\fR a type of C that SG only points at: raw FILE .TP \fBint opterr\fR a variable of C, read and written inside unsafe .TP \fBpublic R name(...)\fR a function of C that other modules may call too .SH DESCRIPTION An \fBextern\fR block, written at the top level of a file, declares functions of C with SG's types. Nothing is written by hand between the two: compiled code calls the function as C does, and the interpreter builds a small library of wrappers with the C compiler and opens it, so \fBsg file.sg\fR and a built program call the same code. Only the libraries of functions the program calls are linked, and a library that is not there is an error before the program runs. .PP The checker cannot see what a function of C does, so every call is written inside \fBunsafe\fR\&. The value it gives is used, or dropped on purpose with \fB_ =\fR, as any other. A function of C is a name of its module, private to it unless \fBpublic\fR is written in front of it. The usual way is to keep the block inside a module that checks what it is given, turns C's codes into SG's types and gives the rest of the program functions with no \fBunsafe\fR\&. .PP C takes integers, \fBfloat\fR, \fBdouble\fR, \fBbool\fR, raw pointers, structs of plain data by value, and \fBref T\fR and \fBread T\fR of plain data that is not an array, which are C's \fBT*\fR and \fBconst T*\fR for the time of the call. A text goes as a \fBread CString\fR, C's \fBconst char*\fR, made with \fBCString.from(text)\fR\&. C gives back a number, a \fBbool\fR, a raw pointer, a struct of plain data, or nothing. \fBbuffer.pointer()\fR, written only as an argument, gives C the items of a list, an array or a slice of plain data for the call. C's \fBlong\fR is \fBCLong\fR in SG, since its size differs between systems. A \fBstring\fR, a \fBchar\fR, a collection and a \fBT?\fR are refused where they are declared, most with a hint of what to write instead. .PP After \fB\&...\fR each value must have a type C reads there: \fBint\fR, \fBuint\fR, \fBlong\fR, \fBulong\fR, \fBnint\fR, \fBnuint\fR, \fBdouble\fR, a raw pointer, a \fBCFunc\fR, \fBref T\fR or \fBread T\fR of plain data, or a \fBread CString\fR\&. C reads the small types as \fBint\fR and a \fBfloat\fR as a \fBdouble\fR, so those are cast first, and a number or \fBnull\fR written bare is refused, since C would read it as whatever type it looks for. .PP \fBextern "./native.c"\fR names a C file in the module's folder or below it. It is compiled with the program, so what cannot be declared, a macro, a \fBstatic inline\fR function, a builtin of the C compiler, is reached through a function written there. All the C files of a program share one unit of C. .PP The compiler cannot check a declaration against the library: it trusts the types written. A function declared with the wrong types is called the wrong way, as in C, and gives whatever the machine leaves. .SH EXAMPLES .SS "Functions of the C library" .RS 4 .nf extern { nuint strlen(read CString text) int toupper(int c) int atoi(read CString text) } void main() { var name = CString.from("pasha") ?? panic("a zero byte inside") print(unsafe strlen(read name)) print((char)unsafe toupper((int)'p')) var count = CString.from("42 cats") ?? panic("a zero byte inside") unsafe { var n = atoi(read count) print(n + 1) } } .fi .RE .PP .RS 4 .nf 5 P 43 .fi .RE .PP \fBextern { }\fR names no library: these are functions of the C library itself. A \fBchar\fR of SG is 32 bits, so the character goes to \fBtoupper\fR as an \fBint\fR and comes back the same way. An \fBunsafe\fR block covers several statements. .SS "A library by its name" .RS 4 .nf extern "m" { double cbrt(double value) double hypot(double x, double y) double nextafter(double from, double toward) } void main() { print(unsafe cbrt(27.0)) print(unsafe cbrt(-8.0)) print(unsafe hypot(3.0, 4.0)) print(unsafe hypot(1e308, 1e308)) print(unsafe nextafter(1.0, 2.0) - 1.0) } .fi .RE .PP .RS 4 .nf 3 -2 5 1.4142135623730951e+308 2.220446049250313e-16 .fi .RE .PP \fB"m"\fR is linked as \fB\-lm\fR, the math library of C. \fBhypot\fR gives the length without squaring first, so it does not overflow where \fBx * x\fR would. .SS "What C takes and gives" .RS 4 .nf extern { Div div(int numerator, int denominator) double modf(double value, ref double whole) raw byte strdup(read CString text) } // C's `div_t`: plain data, laid out as C lays it out. struct Div { int quot int rem } void main() { var d = unsafe div(-17, 5) print($"{d.quot} remainder {d.rem}") double whole = 0.0 var part = unsafe modf(-3.75, ref whole) print($"{whole} and {part}") var name = CString.from("Pasha") ?? panic("a zero byte inside") var copy = unsafe strdup(read name) unsafe { copy[0] = (byte)'p' } print(unsafe string.fromCString(copy) ?? "not UTF-8") unsafe Memory.free(copy) } .fi .RE .PP .RS 4 .nf -3 remainder -2 -3 and -0.75 pasha .fi .RE .PP \fBdiv\fR gives a struct by value, which SG declares with the fields C has, in C's order. \fBmodf\fR writes the whole part through a \fBref double\fR, its \fBdouble*\fR\&. \fBstrdup\fR gives memory of C's \fBmalloc\fR, which the program changes through the \fBraw byte\fR, copies back into a \fBstring\fR and gives back with \fBMemory.free\fR\&. .SS "A varying count of arguments" .RS 4 .nf extern { int snprintf(raw byte target, nuint size, read CString format, ...) } void main() { var line = List.repeat(0, 64) var format = CString.from("%-6s|%5d|%08.3f|%lx") ?? panic("a zero byte inside") var name = CString.from("tea") ?? panic("a zero byte inside") var written = unsafe snprintf(line.pointer(), (nuint)line.length(), read format, read name, (int)42, (double)3.14159, (long)255) print(written) print(string.fromUtf8(line[0..written]) ?? "not UTF-8") var small = List.repeat(0, 8) var needed = unsafe snprintf(small.pointer(), (nuint)small.length(), read format, read name, (int)42, (double)3.14159, (long)255) print(needed) print(string.fromUtf8(small[0..7]) ?? "not UTF-8") } .fi .RE .PP .RS 4 .nf 24 tea | 42|0003.142|ff 24 tea | .fi .RE .PP Each value after the format says its type: \fB%d\fR reads an \fBint\fR, \fB%f\fR a \fBdouble\fR and \fB%lx\fR a \fBlong\fR\&. The format itself is not looked at, so it is the program's to match. \fBline.pointer()\fR lends the list's bytes for the call; into a buffer of 8 bytes \fBsnprintf\fR writes 7 and the zero, and still gives the length the whole line needs. .SS "A C file beside the program" The file bits/native.c: .PP .RS 4 .nf #include #define SQUARE(x) ((x) * (x)) static inline int32_t clamp(int32_t value, int32_t low, int32_t high) { return value < low ? low : value > high ? high : value; } int32_t square_clamped(int32_t value) { return clamp(SQUARE(value), 0, 100); } uint32_t count_bits(uint64_t value) { return (uint32_t)__builtin_popcountll(value); } .fi .RE .PP The file bits/main.sg: .PP .RS 4 .nf extern "./native.c" { int square_clamped(int value) uint count_bits(ulong value) } void main() { print(unsafe square_clamped(7)) print(unsafe square_clamped(12)) print(unsafe count_bits(0xff)) print(unsafe count_bits(ulong.MaxValue)) } .fi .RE .PP .RS 4 .nf $ cd bits $ sg main.sg 49 100 8 64 $ sg build main.sg -o bits $ ./bits 49 100 8 64 .fi .RE .PP A macro, a \fBstatic inline\fR function and a builtin of the compiler cannot be declared in an \fBextern\fR block; a function of the C file reaches all three, and is compiled with the program, so the \fBstatic inline\fR one is inlined. The path starts with \fB\&./\fR and is looked for beside the file of SG. .SS "Behind a safe module" The file calc/number/parse.sg: .PP .RS 4 .nf extern { long strtoll(read CString text, ref raw byte end, int base) } // The number `text` writes in `base`, or `null` when it is not one number that fits. public long? parseIn(read string text, int base) { if base < 2 || base > 36 || text.length() == 0 { return null } var c = CString.from(text) ?? return null raw byte end = null C.setErrno(0) var value = unsafe strtoll(read c, ref end, base) if C.errno() != 0 || unsafe end[0] != 0 { return null } return value } .fi .RE .PP The file calc/main.sg: .PP .RS 4 .nf use number void show(read string text, int base) { var n = parseIn(text, base) if n is null { print($"{text} (base {base}): not a number") return } print($"{text} (base {base}): {n}") } void main() { show("ff", 16) show("-777", 8) show("zz", 36) show("12 apples", 10) show("9223372036854775807", 10) show("9223372036854775808", 10) show("101", 1) } .fi .RE .PP .RS 4 .nf $ cd calc $ sg main.sg ff (base 16): 255 -777 (base 8): -511 zz (base 36): 1295 12 apples (base 10): not a number 9223372036854775807 (base 10): 9223372036854775807 9223372036854775808 (base 10): not a number 101 (base 1): not a number .fi .RE .PP \fBstrtoll\fR is private to \fBnumber\fR, which checks the base before C sees it, and reads what C left: \fBerrno\fR says the number did not fit, and the address \fBstrtoll\fR wrote into \fBend\fR says where it stopped. \fBmain.sg\fR writes no \fBunsafe\fR; naming \fBstrtoll\fR there gets \fB`strtoll` exists in the `number` module but is not public\fR\&. .SH "WHAT THE COMPILER REFUSES" .SS "A text given back" .RS 4 .nf extern { string getenv(read CString name) } void main() { var name = CString.from("HOME") ?? panic("a zero byte inside") print(unsafe getenv(read name)) } .fi .RE .PP .RS 4 .nf error: the C function `getenv` cannot give a `string` --> example.sg:2:5 2 | string getenv(read CString name) | ^^^^^^ hint: C gives a text as `raw byte`; `unsafe string.fromCString(p)` copies it .fi .RE .PP A \fBstring\fR of SG is not ended by a zero byte and owns its memory; what C gives is an address. .SS "A char" .RS 4 .nf extern { int toupper(char c) } void main() { print(unsafe toupper('q')) } .fi .RE .PP .RS 4 .nf error: the C function `toupper` cannot take a `char` a `char` of SG is a Unicode character of 32 bits, and a `char` of C is a byte --> example.sg:2:17 2 | int toupper(char c) | ^^^^ hint: `byte`, or `sbyte` for a C `char` that is signed .fi .RE .SS "A bare number after ..." .RS 4 .nf extern { int printf(read CString format, ...) } void main() { var format = CString.from("%ld seconds\en") ?? panic("a zero byte inside") _ = unsafe printf(read format, 30) } .fi .RE .PP .RS 4 .nf error: this number has no type of its own, and C reads it after `...` as whatever it looks for C reads each value after `...` as the type the function looks for, and only the call says what it gives --> example.sg:7:36 7 | _ = unsafe printf(read format, 30) | ^^ hint: say what C reads: `(int)30`, or `(long)30` where C takes a `long` .fi .RE .PP \fB%ld\fR reads a \fBlong\fR; given an \fBint\fR, C would read half of it and whatever lay beside it. .SS "\&... on a function of SG" .RS 4 .nf int total(int first, ...) { return first } void main() { print(total(1, 2, 3)) } .fi .RE .PP .RS 4 .nf error: a function of SG takes a fixed count of arguments: only a function of C, declared in an `extern` block, takes `...` --> example.sg:1:22 1 | int total(int first, ...) { | ^^^ hint: several values of one type are given as a slice: `read int[] values` .fi .RE .SS "A body in the block" .RS 4 .nf extern { int abs(int value) { return value < 0 ? -value : value } } void main() { print(unsafe abs(-3)) } .fi .RE .PP .RS 4 .nf error: a C function has no body: it is declared here, and C has it --> example.sg:2:24 2 | int abs(int value) { | ^ hint: a function of your own is written outside the `extern` block .fi .RE .SS "A pointer to a buffer kept" .RS 4 .nf extern { int snprintf(raw byte target, nuint size, read CString format, ...) } void main() { var line = List.repeat(0, 32) var at = line.pointer() var format = CString.from("%d") ?? panic("a zero byte inside") _ = unsafe snprintf(at, 32, read format, (int)7) } .fi .RE .PP .RS 4 .nf error: `pointer()` is written only as an argument of a C function C is given the address of the items for the time of the call; a pointer kept after it could point at items that have moved --> example.sg:7:14 7 | var at = line.pointer() | ^^^^^^^^^^^^^^ hint: memory that C keeps is taken with `Memory.allocate(count)` .fi .RE .SS "A function of C as a value" .RS 4 .nf extern { int abs(int value) } void main() { var f = abs print(unsafe f(-3)) } .fi .RE .PP .RS 4 .nf error: `abs` is a function of C, and a `Func` holds the address of a function of SG --> example.sg:6:13 6 | var f = abs | ^^^ hint: wrap the call in a function of your own and use that .fi .RE .PP Where a \fBCFunc\fR is expected, a pointer to a function as C calls it, the name of a function of C is one: \fBCFunc f = abs\fR\&. .SS "A name of C taken twice" .RS 4 .nf extern { int abs(int value) } int abs(int value) { return value < 0 ? -value : value } void main() { print(abs(-3)) } .fi .RE .PP .RS 4 .nf error: function `abs` is defined twice: here, and as a function of C in an `extern` block --> example.sg:5:5 5 | int abs(int value) { | ^^^ .fi .RE .PP A function of C is a name of its module, as a function of SG is. .SS "One function of C, declared two ways" The file clash/a/a.sg: .PP .RS 4 .nf extern { int abs(int value) } public int distanceA(int x) { return unsafe abs(x) } .fi .RE .PP The file clash/b/b.sg: .PP .RS 4 .nf extern { long abs(long value) } public long distanceB(long x) { return unsafe abs(x) } .fi .RE .PP The file clash/main.sg: .PP .RS 4 .nf use a use b void main() { print(distanceA(-3)) print(distanceB(-4)) } .fi .RE .PP .RS 4 .nf $ cd clash $ sg main.sg error: the C function `abs` is declared twice, and the two declarations differ C has one function of this name, so it is declared the same way everywhere --> b/b.sg:2:10 2 | long abs(long value) | ^^^ the other declaration: --> a/a.sg:2:9 2 | int abs(int value) | ^^^ .fi .RE .PP Two modules may declare the same function of C, each for itself, but the same way. C's \fBlabs\fR is the one that takes a \fBlong\fR (\fBCLong\fR in SG). .SS "A C file outside the module" .RS 4 .nf extern "./../shared.c" { int twice(int value) } void main() { print(unsafe twice(4)) } .fi .RE .PP .RS 4 .nf error: `./../shared.c` is not a C file of this module a C file beside the program lies in the module's folder or below it, ends in `.c`, and its path is letters, digits and `_ - . +`, without `..` --> example.sg:1:1 1 | extern "./../shared.c" { | ^^^^^^^^^^^^^^^^^^^^^^ hint: `extern "./native.c" { ... }` .fi .RE .SH "SEE ALSO" .BR sg-unsafe (7), .BR sg-raw (7), .BR sg-export (7), .BR sg-struct (7), .BR sg-public (7), .BR sg-use (7) .PP https://sg\-lang.org/reference/extern.html