Hi,
I've just started using nimrod, and I'm trying to see if I can use it to create static libraries and later compile C code using them. I've tried the following:
nimrod file testec.nim:
proc hello(someone: cstring) {. exportc .} =
echo("Hello, world!", someone)
C file teste.c:
void hello(char* someone);
int main(void)
{
hello("someone there");
return 0;
}
then, compiled with:
nimrod c --app:staticlib --noMain --debuginfo testec.nim
gcc -g -static teste.c -L. -ltestec.nim -ldl -o testenim
(at this point I've got a warning:
I guess it's not a problem, though.)
Then, when I run ./testenim, I got a segfault
> ./testenim
Segmentation fault (core dumped)
I can't understand what I'm doing wrong here. When I did the same, but creating the nimrod proc and calling it from C without arguments, as in:
nimrod file testec.nim:
proc hello() {. exportc .} =
echo("Hello, world!")
C file teste.c:
void hello();
int main(void)
{
hello();
return 0;
}
everything worked. Is there something else I should do here?
Thanks,
André
If you add the switch --header to the first command you will get a testec.h header file with at least the following lines:
N_NIMCALL(void, hello)(NCSTRING someone);
N_CDECL(void, NimMain)(void);
Nimrod code depends on the garbage collector and other stuff that is initialized inside NimMain(). Your second test likely worked because the simpler hello() proc doesn't trigger the gc, string concatenation, or whatever else depends on NimMain(). See if adding the NimMain() call to your first case works.
In general if you are compiling statically it is easier to compile Nimrod code to C and just compile all the files inside the nimcache directory along with your normal code.