Is it possible to avoid giving names to arguments in the functions?
I have the following in the C header:
DLLEXPORT void gr_polyline(int, double *, double *);
and in order to make it work, I have to do:
proc gr_polyline*(a:cint, b:ptr cdouble, c:ptr cdouble) {.importc,dynlib: libName.}
where I have to add a, b, c just to make it work.
#import gr_wrapper
const
libName = "/usr/gr/lib/libGR.so"
proc gr_initgr*() {.importc,dynlib: libName.}
proc gr_polyline*(a:cint, b:ptr cdouble, c:ptr cdouble) {.importc,dynlib: libName.}
proc gr_axes*(a:cdouble, b:cdouble, c:cdouble, d:cdouble, e:cint, f:cint, g:cdouble) {.importc,dynlib: libName.}
proc gr_tick*(a:cdouble, b:cdouble):cdouble {.importc,dynlib: libName.}
proc gr_beginprint*(a:cstring) {.importc,dynlib: libName.}
proc gr_endprint*() {.importc,dynlib: libName.}
when isMainModule:
let x = @[0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
let y = @[0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
let px = cast[ptr cdouble](unsafeAddr(x[0]))
let py = cast[ptr cdouble](unsafeAddr(y[0]))
gr_beginprint("salida.png".cstring)
gr_polyline(x.len.cint, px, py)
gr_axes(gr_tick(0, 1), gr_tick(0, 1), 0, 0, 1, 1, -0.01)
gr_endprint()
#discard readChar(stdin)
gives me:
(a big milestone for somebody like me)
where I have to add a, b, c just to make it work.
Yeah, well you do have to do that. c2nim automates it for you.
When I try to use c2nim, it tends to fail while parsing stuff (vapoursynth and gr). Por example, with gr.h:
$ c2nim --header gr.h gr.h(27, 25) Error: token expected: ; $ c2nim --dynlib /usr/gr/lib/libGR.so /usr/gr/lib/libGR.so(1, 1) Error: invalid token (\127)
so probably I am not using it well.
I write some spaguetty code in python to create the wrapper.
Or alternative, you can point me in the right direction if you actually know what is the problem.
I read the documentation, despite neither here nor here mentions actually how to use it. So I read this.
It is so much information that a newbie (I am not a developper) has to diggest: nim documention (nim is not that easy), learning the minimum necessary from C, learning about libraries themselves, the tooling, ...
So lack of knowledge (I might not understand it even when I read it) and lack of capability (I might have read it but I might have nor retained it) is an issue. I am becoming old also.
This is why I ask for help in a forum (which is for what is meant I guess).
If I were lazy (to read documentation for example) I wouldn't try to learn Nim or to contribute wrapping some libraries. I already feel confortable in python, julia and go, so it is not something like I need Nim.
This sort of answers un-motives newcomers.
$ c2nim --header gr.h
gr.h(27, 25) Error: token expected: ;
AFAIK, c2nim cannot read something with c custom macro. You can edit the header temporarily to remove that custom macro. In this case it's the DLLEXPORT.
Related to your plotting application, also have a look at ggplotnim: https://github.com/Vindaar/ggplotnim .
It helps me make things like:
Also related to your C library wrapping project, have a look at https://github.com/nimterop/nimterop.
Using that, I have successfully wrapped quite a few C libraries, and this is coming from someone who has never coded in C (of course, some learning curve is involved, but the community and the nimterop developer shashlick/genotrance was very helpful as I was learning to use nimterop).
I will take a look to ggplotnim. Looks interesting.
I have played with both nimterop and c2nim before.
Thanks a lot.
To get you started, here's the wrapper output from:
toast -pnrk gr.h
Toast is the c2nim equivalent from nimterop.
@akavel no problem and I understand (I can imagine).
@shashlick thanks for that. Very useful.
I have left my spaguetty code here (I called it h2nim.py). It uses pyclibrary under the hood. It is far from perfect (it is not aiming to), but it is under 500LOC, so it is easy to tweak to particular needs. It might help others wrapping easy libraries. It helped me with VapourSynth because of this issue I filed in nimterop and with GR.
Nimterop has other issues (I should file it). For instance:
toast -pnrk VapourSynth.h
creates the following enums:
....
pfRGB24* = (cmRGB + 10).VSPresetFormat
pfRGB27* = 29.VSPresetFormat
pfRGB30* = 30.VSPresetFormat
pfRGB48* = 31.VSPresetFormat
pfRGBH* = 32.VSPresetFormat
pfRGBS* = 33.VSPresetFormat
while h2nim.py creates:
....
pfRGB24* = (2000010).VSPresetFormat
pfRGB27* = (2000011).VSPresetFormat
pfRGB30* = (2000012).VSPresetFormat
pfRGB48* = (2000013).VSPresetFormat
pfRGBH* = (2000014).VSPresetFormat
pfRGBS* = (2000015).VSPresetFormat
while the original content:
cmRGB = 2000000
...
pfRGB24 = cmRGB + 10,
pfRGB27,
pfRGB30,
pfRGB48,
pfRGBH,
pfRGBS,