voc
voc copied to clipboard
External libs / Foreign function interface
Is any way to link with existing lib (static or dynamic)? May be some foreign function interface (with C support)?
After some investigation I have found that I can declare foreign C function.
PROCEDURE -MyFunc*(a: INTEGER): INTEGER "func(a)";
But how can I implement and/or link it using voc.
Maybe something like
voc func.c main.mod -m
will pass C file to C compiler/linker.
Finally did it. Compile and link output C files (from oberon) + my C files with gcc. It will be good to have instruction for future users.
@dasannikov may you show an exaple? :)
So, You can create Module like this
MODULE MyModule;
IMPORT SYSTEM;
(* include header *)
PROCEDURE -AincludeMyHeader* '#include "MyHeader.h"';
(*add function*)
PROCEDURE -MyFunction*(a:INTEGER): INTEGER "MyFunction(a)";
BEGIN
END MyModule.
And translate it using voc to c files (do not compile and link it) You will have 2 files - MyModule.h and MyModule.c
Additionally you will need C header MyHeader.h
#define INTEGER INT32
#include <SYSTEM.h>
import INTEGER MyFunction(INTEGER a);
And MyHeader.c - implementation file.
#define INTEGER INT32
#include <SYSTEM.h>
INTEGER func(INTEGER a) {
return a / 2;
}
Then you compile an link all this files with GCC/CLANG. It's a little bit complicated because of many params (includes and libs). But here is part of my Makefile if it helps.
all: test01
test01: mymod.c main.c
clang -g -std=c1x -o3 mylib.c main.c mymod.c -otest01 -I"/GitHub/oberon-voc/install/2/include" -L"/GitHub/oberon-voc/install/lib" -lvoc-O2
main.c: main.mod
voc -OC -S main.mod -m
mymod.c: mymod.mod
voc -OC -S -s mymod.mod