Hi,
I'm trying to build a setup in which I could write .dll libraries that I could call from excel. Now, I've managed to make it work with C. There I have two files:
#include <math.h>
double _stdcall square (double *x)
{
return *x * *x;
}
And 2) excelfunctions.def
LIBRARY "excelfunctions"
EXPORTS
squarec = square
looperc = looper
When I compile that with this command, everything works fine and I can call the functions from excel:
/LD /O2 excelfunctions.c /DEF excelfunctions.def
However with Nim, I can't get it to work. I'm not going to show all the iterations I have gone through, but here's one. Currently I have one file, excelnim.nim:
import math
proc square*(x_ptr: pointer): float64 {.stdcall,exportc.} =
var x = cast[ptr float64](x_ptr)[]
return x * x
And I'm trying to compile that with:
c --app:lib --nomain -d:release excelnim.nim
File compiles fine, but Excel tells me that file does not exist (Excel's general error message when something goes wrong with the dll).
I tried to include the .def file, but couldn't find how to do that from compiler manual, forums nor numerous discussions I've been reading through. But I have a lead!
I was comparing how the dll files compare when I open them up with DLL Export Viewer. I can see the functions nicely when I'm opening the dll file I created with C compiler (cl), but when I open the file my nim code compiled into, I can only see NimMain. This is extra confusing since I'm using --nomain in my options...
AFAIK the --noMain doesn't mean "don't generate NimMain()". You still need to NimMain() to boot the GC, execute top-level module code, etc.
I believe what the flag actually does is prevent NimMain() being called in DllMain(), so you have to call NimMain() manually.
Thanks, I changed my code a bit and used {.dynlib.} and it worked! Here's the code I'm using:
import math
proc square*(x: float64): float64 {.stdcall,exportc,dynlib.} =
return x * x