example1:
let t1 = "Hello"
echo repr([ptr string]) #Here is an error,hallo.nims(24, 12) Error: cannot generate VM code for ptr string
example2: let ws2 = loadLib(ws2dll) echo ws2 #Here is an error,hallo.nims(5, 12) Error: cannot generate VM code for ptr string
All reported errors related to the vm,
OS windows7 64bit
nim -v Nim Compiler Version 1.0.0 [Windows: amd64] Compiled at 2019-09-23 Copyright (c) 2006-2019 by Andreas Rumpf
git hash: f7a8fc46c0012033917582eb740dc0343c093e35 active boot switches: -d:release
bbxiong1999: I think the following working code is what you were trying to do:
# example1:
let t1 = "Hello"
var
t2 = t1 # note that this must be indented in order to use the `var` section
t3 : pointer = addr(t2) # note that you need a new line and alignment
# I think the following is what you meant to do...
echo repr(cast[ptr string](t3)[])
# must cast to the "ptr string", then dereference it to print it!
# written more idiomatically in Nim as "cast[ptr string](t3)[].repr.echo:
# example2:
from dynlib import loadLib # must import the function from the library to use it
let ws2 = loadLib("ws2.dll"); echo repr(ws2) # note the argument to `loadLib` is a string
# note the missing `;` to separate statements, and you can't print a "handle"
# which is what is returned from `loadLib`; although you can print a `repr` of it!
On an online compiler (Linux, not that it matters), this produces the following output:
0x7f8cd2672080"Hello"
nil
Conclusion:
I don't know what languages you know after which you were modelling your code, but you need to spend some more serious time with the tutorials and modifying working examples before you are ready to write your own code. In these very simple examples you made the following glaring errors:
The reason for the VM error messages is that it is the VM that runs scripts. Using nim as a compiler will produce better error messages.
The reason that the last printout is nil is that on the machine on which I ran the code, there was no file in the same directory named "ws2.dll" and this is the response when a library can't be loaded for the given file name.
As I said, you need to spend a lot more time modifying and understanding working examples. I don't think this forum is intended as a teaching aid for someone who clearly doesn't yet know how to code.
I see the problem.
import net
. Nims file running error . Nim file is running correctly