External declarations
C functions, variables and types reached by declaring them: no header, no wrapper.
Examples: res\examples\language\external holds small, commented programs for this page's features, each listing the output it prints; run one with adam build <file> -r.
Binding a C function
external replaces a routine's body and binds a symbol another object or library defines. A routine with C linkage and an external clause is the whole binding of a C function, called like any routine:
module exe external_c;
// C's abs, declared with C linkage
func clink abs(const AValue: int32): int32; external "c";
begin
println("{}", abs(-42));
end.
42
The clause names the library as a string, as the name of a string constant, or not at all, when the program's own objects define the symbol.
Another name for a symbol
name "sym" in the clause is the symbol the declaration binds, whatever its Adam name and linkage, so a C function can take a name of your own. A string, a literal or a variable, is passed where C takes a ptr to const char as its bytes and a closing NUL; a wide literal goes where C takes a ptr to const wchar as its UTF-16 units and a NUL unit.
module exe external_name;
// C's strlen under the Adam name CLength
func clink CLength(const AText: ptr to const char): uint64; external "c" name "strlen";
// C's hypot under its own name
func clink hypot(const AX: float64; const AY: float64): float64; external "c";
var
LText: string;
begin
LText := "abcdef";
println("{} {}", CLength("hello"), CLength(LText));
println("{:.1f}", hypot(3.0, 4.0));
end.
5 6
5.0
name is a context word, not a keyword: external name "abs"; names no library, and external name; names the library constant called name.
Finding the library
An external clause's library is found as @linklibrary finds one (Directives). A bare name is searched in each library path, and in each folder the first of libname.dll.a, name.dll.a, libname.a, name.lib, libname.dll and name.dll is used. "name.dll" imports from that DLL, and a path in the name is an error.
The library paths are, in order:
- every module's
@addlibrarypathfolders; - the build's own output lib folder, so a program links an Adam lib built into the same output by its name alone;
- the target's vendor and system library folders.
Callbacks
An Adam routine named as a value fills a parameter of a plain func type, so C can call it back. Here C's qsort sorts an Adam array through an Adam comparator; the library that holds qsort differs by target, so @ifdef picks the declaration:
module exe external_callback;
type
// the comparator qsort calls, a plain func type
Compare = func(const AA: ptr to const int32; const AB: ptr to const int32): int32;
@ifdef TARGET_X86_64_WINDOWS_GNU
func clink qsort(const ABase: ptr; const ACount: uint64; const ASize: uint64;
const ACompare: Compare); external "ucrtbase";
@else
func clink qsort(const ABase: ptr; const ACount: uint64; const ASize: uint64;
const ACompare: Compare); external "c";
@endif
// rising order: negative, zero or positive, as C expects
func Rising(const AA: ptr to const int32; const AB: ptr to const int32): int32;
begin
return AA^ - AB^;
end;
var
LItems: array[0..4] of int32;
begin
LItems[0] := 5;
LItems[1] := 2;
LItems[2] := 8;
LItems[3] := 1;
LItems[4] := 3;
qsort(addr of LItems, 5, 4, Rising);
println("{} {} {} {} {}", LItems[0], LItems[1], LItems[2], LItems[3], LItems[4]);
end.
1 2 3 5 8
A closure that captures reaches a C callback slot through CCallback<T>, a plain C function pointer of the func type T: C callbacks.
C variadics
The arguments past a C variadic's fixed parameters take C's default promotions: bool, the integers narrower than 32 bits and char reach C as int32, float32 as float64, and int64, uint64 and a C string as they are. The ... takes plain values only: a string, an object, an interface, a task, a sequence, a varargs or a mutex there is an error, and a string goes as a ptr to const char. Variadics written in Adam are on Variadics.
Variables, objects and extends
An external variable is declared at module level only, and array[0..] of T declares one whose length C sets. An object type followed by external "lib"; is an object type another binary defines, and extend T .. end; external "lib"; binds the methods another binary adds to T. A generic type or extend is never external.