Hello,
I consider myself a newish nim programmer with some success and some failures. I had yet to dabble in the FFI and started tinkering around with it. I have an external .dylib and its corresponding .h. I am attempting to test out calling from nim. I think everything has been going ok until I encountered an int *.
What can I use in nim to pass as the equivalent C type?
Any thoughts or help on this would be greatly appreciated.
C Declaration
int Api_Version_V2(char *version, int *length);
Nim Code
const MY_LIB = "api.dylib"
proc Api_Version_V2(version: cstring, length: ???): cint {.importc, dynlib: MY_LIB, header: "Api.h", discardable.}
proc getVersion(): string =
result = newString(64)
Api_Version_V2(result.cstring, ???)
result = $(result.cstring)
echo getVersion()
Run Snippet
nim c -p:./ -r CallC.nim -d:nimDebugDlOpen
ptr cint or var cint depending on what the API describes. If it's supposed to be just "int passed as reference" var cint is better, if it's supposed to be "some safely allocated integer" than ptr cint is better.
For ptr cint you need to do myint.addr if you want to pass a stack allocated variable, for var cint you just pass an integer declared as a variable.