BackGround
In fact, I am porting FLTK C 1.3.3 for FreeBASIC to nimlang. Please note FLTK C 1.3.3 for FreeBASIC is a C interface upon the FLTK in CPP.
Many functions in DLL meet the same similar name format, for example
#inclib "fltk-c-1.3.3-64" ' Windows 64-bit
function Fl_ButtonExNew (byval x as long, byval y as long, byval w as long, byval h as long, byval title as const zstring ptr=0) as Fl_ButtonEx Ptr
sub Fl_ButtonExDelete(byval x as Fl_ButtonEx ptr)
function Fl_BoxExNew (byval x as long, byval y as long, byval w as long, byval h as long, byval title as const zstring ptr=0) as Fl_BoxEx Ptr
sub Fl_BoxExDelete(byval x as Fl_BoxEx ptr)
and so on
and it seems that the delcared function/sub will load the function of same name in fltk-c-1.3.3-64.dll automatically( correct me if am wrong)
Solution in FreeBASIC
So in the FreeBASIC header fltk-main.bi
, there is an aided macro
#macro DeclareEx(_name_)
declare function _name_##ExNew(byval x as long, byval y as long, byval w as long, byval h as long, byval title as const zstring ptr=0) as _name_##Ex ptr
declare sub _name_##ExDelete (byref ex as _name_##Ex ptr)
#endmacro
by the help of which, the above code( and many other codes) can be generated by a simple one line code:
DeclareEx(Fl_Button)
DeclareEx(Fl_Box)
My question in nimlang
In nimlang( please ignore the number type conversion for the time being), the above code can be translated by hand as
const fltk = "fltk-c-1.3.3-64.dll"
type long = int64
proc Fl_ButtonExNew (x: long, y: long, w: long, h: long, title: cstring=nil): Ptr Fl_ButtonEx {.cdecl, importc: "Fl_ButtonExNew", dynlib: fltk, discardable.}
proc Fl_ButtonExDelete(x: ptr Fl_ButtonEx) {.cdecl, importc: "Fl_ButtonExDelete", dynlib: fltk, discardable.}
proc Fl_BoxExNew (x: long, y: long, w: long, h: long, title: cstring=nil): Ptr Fl_BoxEx {.cdecl, importc: "Fl_BoxExNew", dynlib: fltk, discardable.}
proc Fl_BoxExDelete(x: ptr Fl_BoxEx) {.cdecl, importc: "Fl_BoxExDelete", dynlib: fltk, discardable.}
So I try to mimic what the FreeBASIC Macro does as
const fltk = "fltk-c-1.3.3-64.dll"
type long = int64
template DeclareEx*(name: untyped) {.dirty.}=
type `name Ex` = object
type `name ExNew`* = proc(x: long, y: long, w: long, h: long, title: cstring=nil): ptr `name Ex` {.cdecl, importc: "name New", dynlib: fltk, discardable.}
type `name ExDelete`* = proc(ex: ptr `name Ex`) {.cdecl, importc: "name ExDelete", dynlib: fltk, discardable.}
DeclareEx(Fl_Button)
But when I compile it, I get
d.nim(9, 10) template/generic instantiation of
DeclareEx
from hered.nim(6, 118) Error: invalid pragma: importc: "name New"
So, any solution? Thanks