Hello,
I continue my tests on nim, and in particular the generation of a C library that will be reused in another project. I've been able to make some exports using the documentation examples and @yardanico 's code.
But when I took over my old code , I encountered a problem that I didn't have in the old versions (if I remember well. It's a code that I left aside for a few months). I'm pretty sure that this worked some versions ago (1.0.6? with a tiny difference about the struct cpoint if i'm not wrong). But i'm facing this crash, don't know really why. Is I have to manage the memory now?
Any ideas? Thanks
I took some time (sorry for the delay...) to do tests.
You can find logs for FreeBSD with nim 1.2.6. I add a new c code with initialized points. However I also try this code on Linux (Debian 10 with nim 1.2.6 via choosenim) and it doesn't work (same crash with or without malloc).
Sorry, have never used Nim from C, so I don't really understand it...
In one of you linked files you have
cpoint *p2;
printf("%lf %lf\n", getX(p2), getY(p2)); // crash SIGSEGV: Illegal storage access. (Attempt to read from nil?)
puts("la\n");
setX(p2, 5);
setY(p2, 5);
How does memory allocation for the point work in this case?
in the other one
NimMain();
//cpoint *p1;
cpoint *p1 = malloc(sizeof(cpoint*));
printf("%lf %lf\n", getX(p1), getY(p1));
setX(p1, 2);
setY(p1, 2);
I was able to avoid C for a long time, but I think instead of malloc(sizeof(cpoint*)) you may want malloc(sizeof(cpoint))
malloc(sizeof(cpoint)) won't compile as cpoint is an incomplete type
but sizeof(cpoint*) isnt enough, struct cpoint is a pointer and two floats and you'd never know that just from the header
the thing to do is use {.bycopy.}:
#cpoint.nim
type
cpoint*{.exportc,bycopy.} = PointXY[float]
so that the full cpoint struct gets emitted in the header file
then you can
cpoint p1;
printf("%f %f",p1.x,p1.y);
p1.y = 5.0;
setX(&p1);
to your hearts content