Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
XP is just a number
 
PerlMonks  

Calling C++ functions from Perl without using xsubpp

by dafrito (Initiate)
on Sep 18, 2012 at 20:56 UTC ( #994363=perlquestion: print w/ replies, xml ) Need Help??
dafrito has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a Perl-to-C++ bridge, and I've ran into a roadblock regarding calling arbitrary C++ functions from Perl. As I understand it, XS would be used to compile headers to allow invocation from Perl. However, I would like this bridge to work without the need to compile separate headers. Let me explain firstly why I don't think this is crazy:

When Perl invokes a C function, it provides a stack of arguments. Through the use of C++ tuples and function overloading, I would be able to convert each Perl argument to the correct C++ type. Once all arguments have been converted, I would then invoke the target C++ function.

So, my question is: How do I pass a C++ function into Perl? Specifically, what function should be used to pass my function pointer into perl, and what should the signature of that function look like?

I'll start looking into code generated by xsubpp, but I'd love some pointing in the right direction if you guys have any.

Thanks in advance!

Comment on Calling C++ functions from Perl without using xsubpp
Re: Calling C++ functions from Perl without XS
by Anonymous Monk on Sep 18, 2012 at 21:13 UTC
Re: Calling C++ functions from Perl without using xsubpp
by Corion (Pope) on Sep 18, 2012 at 21:13 UTC

    Have you looked at Inline::CPP, which allows you to seamlessly call C++ code from Perl and likely also Perl code from C++?

Re: Calling C++ functions from Perl without using xsubpp
by davido (Archbishop) on Sep 18, 2012 at 21:40 UTC

    Inline::CPP does pretty much what you're asking. It doesn't completely absolve you from studying perlguts, but it's a big step in that direction.

    use Inline CPP => 'DATA'; my $sum = add( 2, 4 ); print "$sum\n"; __DATA__ __CPP__ #include <iostream> int add( int x, int y ) { return x + y; }

    There are C++ constructs that don't map well to Perl. For one thing, your C++ compiler has no idea how Perl intends to call a function, so template-based functions and classes need to be instantiated in a concrete C++ function or class wrapper before being exposed to Perl. But once you get familiar with the large subset of C++ that does map well to Perl, Inline::CPP is pretty fun to use. I've even spent some time verifying its compatibility with the new C++11 standard (much of it "just works" as it should, with a few exceptions in predictable places).

    The Inline::CPP documentation (always a work in progress) is improving, and should get you going in the right direction. I also recommend reading Inline, Inline::C, and Inline::C-Cookbook. Lots of reading, I know. But each of those resources gets you closer to being productive in this funky art. The Inline::C-Cookbook, in particular, demonstrates how to manipulate the Perl stack within your C or C++ functions.

    Also, the tests found in the Inline::CPP distribution under t/ and grammar/t demonstrate many C++ constructs at work. And Math::Prime::FastSieve is a sort of "proof of concept" and example of using Inline::CPP.

    Advanced Perl Programming (2nd Edition) has a chapter that discusses the Inline modules (Inline::C in particular, but much is applicable to Inline::CPP). And Sam Treagar's book, "Writing Perl Modules for CPAN" has a chapter on Inline::C too.

    If you find yourself working much with it, feel free to join the inline@perl.org mailing list. See http://lists.perl.org for details.


    Dave

Re: Calling C++ functions from Perl without using xsubpp
by andal (Friar) on Sep 19, 2012 at 08:36 UTC

    Actually, xsubpp produces set of code that does conversion from structures specific to perl to those understandable for your code. In fact, you can write C code that works with perl structures directly, in which case no conversion has to be done. Just make your function work with AV* or for example SV*.

    Inline::C and Co. provide automatic conversion to some set of common types, just like XS does. If using these types is enough, then you don't need anything else but those modules. If you have to work with more complex data structures, then you have to understand perl's guts. Read perldoc perlguts. Then read perldoc perlxs to learn how to work with XS. Then read perldoc perlapi to see which functions are available to you for working with perl's structures. XS shall only simplify certain things for you.

    After all, you can think of Perl as of C library providing certain API.

      Inline::C and Co. provide automatic conversion to some set of common types, just like XS does. If using these types is enough, then you don't need anything else but those modules. If you have to work with more complex data structures, then you have to .... learn how to work with XS.

      Perhaps I've misread (and inappropriately edited) that excerpt from your post, but it seems to me that you're saying that Inline::C is good only for some limited set of C types.
      That's not the case - if it can be done using XS then it can be done using Inline::C. After all, Inline::C is little more than a utility that generates an XS file, which it then compiles.
      (I keep thinking there must be *some* feature of XS that Inline::C can't reproduce ... and I'd love to see some examples that validate such thinking.)

      Cheers,
      Rob

        Well, I guess I have expressed myself imprecise. My point was not so much about abilities of Inline::C. My point was about need to learn perl guts if one needs to work with more complex input/output parameters. In this case learning XS comes in mostly for understanding what is happening. Though, probably some people can write code without knowing how their code turns into program :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994363]
Front-paged by bulk88
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2014-04-21 13:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (495 votes), past polls