Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly create a nim/nimrod windows dll

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?

like image 734
enthus1ast Avatar asked Sep 29 '15 08:09

enthus1ast


People also ask

Does Nim compile to C?

Nim's FFI is used to call functions written in the other programming languages that it can compile to. This means that libraries written in C, C++, Objective-C, and JavaScript can be used in the Nim source code.

How do I run a NIM code?

On Linux we can run our program by typing ./helloworld in the terminal, and on Windows we do it by typing helloworld.exe . c is telling Nim to compile the file, and -r is telling it to run it immediately. To see all compiler options, type nim --help in your terminal.

Where is Nim CFG?

nim. cfg that resides in the same directory as $project. nim. This file can be skipped with the --skipProjCfg command-line option.


1 Answers

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
like image 84
enthus1ast Avatar answered Sep 28 '22 03:09

enthus1ast