timotheecour / D_vs_nim

comparison of D vs nim
Apache License 2.0
63 stars 14 forks source link

C interop #30

Closed Laeeth closed 5 years ago

Laeeth commented 5 years ago

D can just include C headers using dpp. I guess Nim can do so natively?

timotheecour commented 5 years ago

dpp is a separate binary that must be run in a preprocessing step though.

Nim can do it natively, here are some examples:

# from a header, with optional rename
proc c_fprintf*(f: CFilePtr, frmt: cstring): cint {.importc: "fprintf", header: "<stdio.h>", varargs, discardable.}
# from a dynamically loaded lib
proc foobar*() {.importc, dynlib: "libmyfun.dylib".}

# from embedded code (a bit similar to how go does c interop)
proc foobar2() {.importc.}
{.emit:"""
#include <stdio.h>
void foobar2(){
  printf("in foobar2\n");
}
""".}