I have a C library clib.c
with this function
int hi(char* hello) { return 900; }
compiled as:
gcc clib.c -o clib.so --shared -fPIC
I'm consuming this in a Nim libray called 'nlib.nim`:
proc hi*(hello: cstring): cint {.cdecl, importc: "hi", dynlib: "./clib.so".}
proc hi2*(hello: cstring): cint {.cdecl, exportc.} = return hi(hello)
complied as:
nim c --app:lib --noMain -o:nlib.so nlib.nim
If I call the hi2 function directly in Nim, it returns 900, perfectly. But if I call it from NodeJS via FFI:
var ffi = require('ffi');
var lib = ffi.Library('./nlib.so', { 'hi2' : [ "int", ["string"] ] });
console.log(lib.hi2("hey"));
I get a Segmentation fault (core dumped)
.