Did not generate C/C++ expression; you may not need `cexpr` here
mantielero opened this issue · comments
mantielero commented
I was playing with OpenSceneGraph. I wrote the following code:
{.passL: "-losg -losgViewer",
passC:"-I/usr/include/osg" }
import cinterop
csource "Vec3f":
cnamespace osg:
type
Vec3f* = object of CClass
proc newVec3[X,Y,Z:SomeNumber](x:X, y:Y, z:Z):Vec3f =
Vec3f.init(x.cfloat, y.cfloat, z.cfloat)
proc x*(this:Vec3f):cfloat =
cexpr[cfloat]^this.x()
var p = newVec3(1, 2.0, 3.0)
echo p.x()
This code compiles, but I get a warning:
Warning: did not generate C/C++ expression; you may not need `cexpr` here
The code compiles, but produces a runtime error:
Error: call depth limit reached in a debug build (2000 function calls). You can change it with -d:nimCallDepthLimit=<int> but really try to avoid deep recursions instead.
Error: execution of an external program failed: '/home/jose/src/osg.nim/prueba2 '
The source code for the header is: Vec3f
Ryan Blonna commented
That looks like an infinite recursion:
proc x*(this:Vec3f):cfloat =
cexpr[cfloat]^this.x()
cexpr
will pick up a symbol if it exists and will print that warning; it will generate the symbol if it doesn't.
If you want to wrap cinterop
with your own proc
s, you need to use different names to avoid recursion. You can either rename your x
function, or use normal importcpp
instead of cexpr
:
proc x*(this:Vec3f):cfloat {.importcpp:"(#.$1())".}
Check out the Nim manual on importcpp
, it's pretty cool.
mantielero commented
Understood. Thanks a lot.