public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
719fcd5bcb6c6fa6749bed3beb8d432f360e8c7e blob 1888 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
 
@node C FFI
@chapter C FFI

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 @code{gcc} but you can use the compiler/linker of
your preference.

@section Calling C from O

To call external code you can use the @code{extern} statement.

@subsection extern

@verbatim
# file: answer.ol

extern fn u32print(number: u32): u32

fn main(): u8 {
  u32print(42)

  return 0
}
@end verbatim

That way instead of defining the @code{u32print} function. It must be provided
at the linkage step.

@verbatim
// file: u32print.c

#include <stdio.h>

int u32print(int number) {
  return printf("%d\n", number);
}
@end verbatim

@subsection Compiling without linking

If you try to compile the answer.ol file you will receive the follow error:

@verbatim
olc answer.ol -o answer.o
/usr/bin/ld: answer.o.o: in function `main':
(.text+0x10): undefined reference to `u32print'
collect2: error: ld returned 1 exit status
@end verbatim

That's because @code{O} tries to link by default. To assemble the code without
linking it, you can use the @code{-c} option.

@verbatim
olc answer.ol -c -o answer.o
@end verbatim

After that you can do the same with the C file that contains the function that
you are looking to call.

@verbatim
gcc u32print.c -c -o u32print.o
@end verbatim

And then you can link both object files into an executable:

@verbatim
gcc answer.o u32print.o -o answer
@end verbatim

@subsection Calling O from C

All that is required is to set the function prototype.

@verbatim
# file: sum.ol

fn sum(a: u32, b: u32): u32 {
  return a + b
}
@end verbatim

@verbatim
# file: csum.c

#include <stdio.h>

int sum(int a, int b);

int main() {
  printf("%d\n", sum(41, 1));
  return 0;
}
@end verbatim

Compiling:

@verbatim
olc sum.ol -c -o sum.o
gcc sum.o csum.c -o csum
@end verbatim
debug log:

solving 719fcd5 ...
found 719fcd5 in http://lists.johnnyrichard.com/olang/20241106041055.194975-3-carlos@maniero.me/

applying [1/1] http://lists.johnnyrichard.com/olang/20241106041055.194975-3-carlos@maniero.me/
diff --git a/docs/info/c-ffi.texi b/docs/info/c-ffi.texi
new file mode 100644
index 0000000..719fcd5

Checking patch docs/info/c-ffi.texi...
Applied patch docs/info/c-ffi.texi cleanly.

index at:
100644 719fcd5bcb6c6fa6749bed3beb8d432f360e8c7e	docs/info/c-ffi.texi

Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox