Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load function in Nim dll

I have two files:

foo.nim:

 proc double*(x: cint): cint
    {.cdecl, exportc: "double", dynlib.} =
    return x * 2

bar.nim:

proc double(x: cint): cint
  {.cdecl, dynlib: "foo.dll", importc.}

echo double(2)

I compile foo.nim by running nim c --app:lib foo.nim, which generates foo.dll. When I run bar.nim, I expect it to load double from foo.dll and print 4, but I get this error:

could not import: double

dllexp.exe only shows NimMainInner, so the function is not even being exported.

How can I import the double function from the dll? I plan on loading functions that might have the same name from multiple dlls, so a solution that can handle that would be preferred. I am running this on Windows 7.

like image 615
pengowen123 Avatar asked Sep 11 '26 14:09

pengowen123


1 Answers

double is reserved word in C. And nim compiles "double" function to something like this:

extern "C" __declspec(dllexport) int double(int x0);

Somehow vcc can compile such code, but gcc don't. But there is a workaround solution to rename "double" symbol during link stage

proc double*(x: cint): cint
    {.cdecl, exportc: "_double", dynlib.} =
    return x * 2

{.emit: """
#pragma comment(linker, "/export:double=_double")
""".}

The resulted dll will have original "_double" and "double" symbols

like image 109
ivan2kh Avatar answered Sep 13 '26 02:09

ivan2kh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!