Hello,
I'm trying to make a Wren wrapper for Nim, but I have trouble understanding Nim's FFI. So far I've got a folder structure like this:
src/
- wren.nim
- wren/
- incl/
- wren.h
- wrapper.nim
- vm/(...)
The src/wren/vm folder's contents can be found here: https://github.com/wren-lang/wren/tree/master/src/vm
I tried using the importc macro in wrapper.nim like this:
# tell the C compiler to look for the wren header
# unfortunately this must be done as Nim doesn't copy the .h filed from the header pragma to its compile cache
from os import splitPath
{.passC: "-I" & currentSourcePath.splitPath.head.}
const h = "wren.h"
type
WrenConfiguration* {.importc, header: h.} = object
proc wrenInitConfiguration*(cfg: ptr WrenConfiguration) {.importc: "wrenInitConfiguration", nodecl.}
However, when I try and test if the wrenInitConfiguration proc works, I get this error:
/usr/bin/ld: /home/[username]/.cache/nim/test1_d/wren_test1.c.o: in function `NimMainModule':
wren_test1.c:(.text+0x95e): undefined reference to `wrenInitConfiguration'
collect2: error: ld returned 1 exit status
What am I doing wrong, and how can I make a wrapper properly?
Use compile pragma or link pragma (below it) to point where the actual implementation code/binary available.
Minimum example
#include <stdio.h>
void greet() {
printf("hello from sub\n");
}
sub.c
proc greet() {.importc.}
{.passC: "-I.".}
{.compile: "sub.c".}
proc main =
greet()
main()
main.nim
compile it as usual
$ nim c -r main.nim