How to correctly create a nim/nimrod windows dll

时光怂恿深爱的人放手 提交于 2019-12-23 10:59:05

问题


I want to create a dll from nim code. But i failed to register some other exports than "NimMainInner". Even if i try this simple example its not working:

proc Hellow(): cint {.exportc.} =
  echo("hello")
  return 1

i've compiled it with nim c --app:lib libh4x.nim and nim c -d:release --app:lib --no_main libh4x.nim

i use Nim Compiler Version 0.11.2 (2015-05-04) [Windows: i386]

to inspect the dll i use dllexp.exe. I've also tried to load the dll with python ctypes, but none of my exports are shown or are callable. I can see the proc name in the resulting dll with an hexeditor, though.

What have i missed here?


回答1:


The dynlib pragma was missing. So i changed the definition to:

proc Hellow(): cint {.exportc,dynlib.} =
  echo("hello")
  result = 1

now it works.

Note: If you use this with pythons ctypes and with function parameters make sure to use ctypes.cdll.LoadLibrary instead of ctypes.windll.LoadLibrary: Python ctypes argument errors

and to declare the function like this:

proc myinit(procid : int) {.cdecl,exportc,dynlib.} =
  discard


来源:https://stackoverflow.com/questions/32839321/how-to-correctly-create-a-nim-nimrod-windows-dll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!