Anyone know the recommended way to do interop with C and Nimrod? I've been doing Project Euler problems in Nimrod, and wanted to use some library functions from GSL for bignum support, but I couldn't really get c2nim to work with the header files.
I think that I could use the 'importc'/'dynlib' with the compiled library, but there's not a whole lot of documentation on that in the Nimrod manual, and I ended up just doing that problem in C because it was easier (read: I was lazier) to get working.
To the curious - I got this working (thank you def- for the rosetta code examples!). Point of correction - the library I was actually using for bignum support was GMP, not GSL. (I was getting confused because I used functions from GSL for a different problem).
Importing a standard procedure from a C library like GMP actually is pretty easy (use the importc/header pragmas).
Here's what tripped me up:
* Calling variadic functions from Nimrod (just wrote a wrapper)
* Getting to link against gmp (used the passL: "-lgmp" pragma)
* Remembering to free what was allocated by C library functions (can't GC what you didn't allocate) - caught with valgrind
I'll probably push to my github repo, but can post to a pastebin or something if there's interest in a less cookie-cutter example
Comments
Anyone know the recommended way to do interop with C and Nimrod? I've been doing Project Euler problems in Nimrod, and wanted to use some library functions from GSL for bignum support, but I couldn't really get c2nim to work with the header files.
I think that I could use the 'importc'/'dynlib' with the compiled library, but there's not a whole lot of documentation on that in the Nimrod manual, and I ended up just doing that problem in C because it was easier (read: I was lazier) to get working.
For general C/Nimrod interop see these Rosetta Code tasks:
http://rosettacode.org/wiki/Call_a_foreign-language_function...
http://rosettacode.org/wiki/Call_a_function_in_a_shared_libr...
http://rosettacode.org/wiki/Use_another_language_to_call_a_f...
Making c2nim work with header files can be a bit difficult. The main information is here: http://nimrod-lang.org/c2nim.html
When something throws an error I comment it out and try to see if I can fix it up by hand.
To the curious - I got this working (thank you def- for the rosetta code examples!). Point of correction - the library I was actually using for bignum support was GMP, not GSL. (I was getting confused because I used functions from GSL for a different problem).
Importing a standard procedure from a C library like GMP actually is pretty easy (use the importc/header pragmas).
Here's what tripped me up:
* Calling variadic functions from Nimrod (just wrote a wrapper)
* Getting to link against gmp (used the passL: "-lgmp" pragma)
* Remembering to free what was allocated by C library functions (can't GC what you didn't allocate) - caught with valgrind
I'll probably push to my github repo, but can post to a pastebin or something if there's interest in a less cookie-cutter example
I never tried the c2nim thing but just used a few compiler directives I think to call C code. It was incredibly easy.