proc nim_init*() = var cmdLine{.importc: "cmdLine".}: array[0..255, cstring] cmdCount{.importc: "cmdCount".}: cint init(addr(cmdLine), addr(cmdCount))
So I used it last night in a similar way, but got always 0 and nil as result. So I created this test program:
#var # does work fine
# cmdCount {.importc: "cmdCount".}: cint
# cmdLine {.importc: "cmdLine".}: cstringArray # char**
proc nux() =
var # does not work
cmdCount {.importc: "cmdCount".}: cint
cmdLine {.importc: "cmdLine".}: cstringArray # char**
echo cmdCount#, cmdLine[0]
if cmdLine == nil:
echo "cmdLine == nil"
nux()
output is:
$ ./ttt "aa" 1 bb 0 cmdLine == nil
cmdCount and cmdLine seems to work only when defined outside of the proc. This is for Nim Compiler Version 0.11.3 (2015-08-11) [Linux: amd64].
When defined in the proc, nimcache code is
N_NIMCALL(void, nux_92003)(void) { int cmdCount; NCSTRING* cmdLine; NimStringDesc* LOC1; nimfr("nux", "ttt.nim") cmdCount = 0; cmdLine = 0;
I guess that is indeed the intended behaviour. Maybe compiler should give a warning when using that syntax in a proc? And maybe some wrappers need fixes?
You can work around this problem by using {.importc, global.}. Example:
{.emit: """
int foovar = 314;
""".}
proc main() =
var foovar {.importc, global.}: cint
echo foovar
main()
In the long term, it might be a good idea if {.importc.} implied global.