O programming language follows C’s calling conventions, meaning that every O function can be called by C without any extra effort.
All example bellow will use gcc
but you can use the compiler/linker of
your preference.
To call external code you can use the extern
statement.
# file: answer.ol extern fn u32print(number: u32): u32 fn main(): u8 { u32print(42) return 0 }
That way instead of defining the u32print
function. It must be provided
at the linkage step.
// file: u32print.c #include <stdio.h> int u32print(int number) { return printf("%d\n", number); }
If you try to compile the answer.ol file by using the
olc answer.ol -o answer.o
command you will receive the follow error:
/usr/bin/ld: answer.o.o: in function `main': (.text+0x10): undefined reference to `u32print' collect2: error: ld returned 1 exit status
That’s because O
tries to link by default. To assemble the code without
linking it, you can use the -c
option.
olc answer.ol -c -o answer.o
After that you can do the same with the C file that contains the function that you are looking to call.
gcc u32print.c -c -o u32print.o
And then you can link both object files into an executable:
gcc answer.o u32print.o -o answer
All that is required is to set the function prototype.
# file: sum.ol fn sum(a: u32, b: u32): u32 { return a + b }
// file: csum.c #include <stdio.h> int sum(int a, int b); int main() { printf("%d\n", sum(41, 1)); return 0; }
Compiling:
olc sum.ol -c -o sum.o gcc sum.o csum.c -o csum