Hello.
I was reading the "Nimrod plays nice with other languages" and decided to test some things in Nimrod. I'm looking for a high level language which I can compile to C/C++ so I can write the app logic in it and target Win32, Mac, android, ios, etc, platforms.
I did modify the hello world tutorial like this:
proc ask(question: string) =
# This is a comment
echo(question)
var name: string = readLine(stdin)
echo("Hi, ", name, "!")
ask("What's your name?")
Inspecting the generated code in nimcache looks quite obfuscated and not easy to call. So I guess all the "plays nice" features are to call other languages from nimrod, not the other way round. Is this correct or did I miss a compiler switch to make easier nimrod-code interfaces for C?
Is this correct or did I miss a compiler switch to make easier nimrod-code interfaces for C?
Well you missed quite a lot. ;-) For a start, mark the procs that you want to export with exportc to keep the compiler from optimizing them away and prevent name mangling. Then compile with --no_main and perhaps --no_linking. In the C code you need to call void NimMain(void) to setup Nimrod's GC and module initialization code.
The examples/lazarus directory contains an example how to mix Nimrod with Lazarus; it shows all the details and should be easy to transfer to C/C++.
You can also tell the compiler to produce a dynamic or static library with --app:lib and --app:staticlib
Currenly, you must manually create a header file listing the exported functions, but it woudn't be too hard to add support for automatic generation of these headers in the compiler.
Araq The examples/lazarus directory contains an example how to mix Nimrod with Lazarus; it shows all the details and should be easy to transfer to C/C++.
The lazarus example fails because nimlaz.rc file is missing in source tree.
I was looking at the types section of the manual and found the CString for C interfacing. However I wasn't able to find any table showing a mapping between nimrod types and C level types or guarantees for type conversions like structs (ie. does the struct in memory match any memory layout/padding). Where should I look?
The lazarus example fails because nimlaz.rc file is missing in source tree.
I think Lazarus can easily re-generate it. But I added it anyway.
Hrm, the docs are indeed spread on this topic:
http://nimrod-code.org/manual.html#foreign-function-interface
http://nimrod-code.org/manual.html#pure-pragma
http://nimrod-code.org/nimrodc.html#dll-generation
You can also run c2nim on some code snippets to see how to wrap structs etc.
windres: can't open file 'nimlaz.manifest': No such file or directory
Maybe I've got a broken/incompatible version of Lazarus? I got 0.9.30.4 from http://www.lazarus.freepascal.org/ for windows 32 bits. Anyway, it's not that I'm interested in running the sample, after all I don't know anything about Pascal and the interesting bits I already understood from this thread. Thanks.