Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Inline CPP function not found

by markov (Scribe)
on May 16, 2011 at 20:30 UTC ( #905148=perlquestion: print w/replies, xml ) Need Help??
markov has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I try to get this to work:
use Inline CPP => Config => ENABLE => 'STD_IOSTREAM'; use Inline CPP; print "g = ", g(12), "\n"; my ($a, $b, $c) = (1, 2, 3); my ($x, $y, $z); my $error = f($a, $b, $c, $x, $y, $z); print " --> $x, $y, $z (e=$error)\n"; __END__ __CPP__ #include <iostream> using namespace std; int f(double a, double b, double c, double& x, double& y, double& z) { int error = -2; x = a +42; y = b +42; z = c +42; return error; } int g(int b) { return 42; }
But the output is
g = 42 Use of inherited AUTOLOAD for non-method main::f() is deprecated at ./ +z line 12. Can't locate auto/main/ in @INC (@INC contains: ...)
How do I get this to work? Thanks in advance.

Replies are listed 'Best First'.
Re: Inline CPP function not found
by ikegami (Pope) on May 16, 2011 at 21:00 UTC

    Does it run if you switch double& to double? If so, it's a lack of entry in the typemap to handle double&.

    Update: I managed to get Inline::CPP installed (with a patch from a bug report), and confirmed that the problem is indeed that double& isn't handled. I was able to make the code work using the following:

    int f(double a, double b, double c, SV* x, SV* y, SV* z) { int error = -2; SvUPGRADE(x,SVt_NV); SvNV_set(x,a+42); SvNOK_on(x); SvSETMAGIC(x); SvUPGRADE(y,SVt_NV); SvNV_set(y,b+42); SvNOK_on(y); SvSETMAGIC(y); SvUPGRADE(z,SVt_NV); SvNV_set(z,c+42); SvNOK_on(z); SvSETMAGIC(z); return error; }

    It may also be possible to keep the original prototype by creating a typemap entry for double&.

      Very useful, thank you. With a wrapper I can call the original function with it.
Re: Inline CPP function not found
by educated_foo (Vicar) on May 16, 2011 at 21:01 UTC
    As far as I can tell from Inline::CPP::grammar, Inline::CPP doesn't handle reference parameters, so it's probably skipping your function. Try using pointers instead.

    EDIT: I'm wrong -- it does at least parse it, so while I'm pretty sure Inline::CPP is skipping it as something it can't handle, I'm not sure what the problem is.

      It think the parser does understand it. In Inline/CPP/
      type1: TYPE star(s?) star: '*' | '&'
      So, if 'double *' is parsed, than 'double &' as well. Probably I do need to do something with typemaps or function rewrites. Would be nice if someone knows what.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://905148]
Approved by Eliya
NodeReaper settles into the armchair by the fire. You weren't using it were you?

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2017-09-24 14:41 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (274 votes). Check out past polls.