Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Writing XS frontends to C++ libraries.

by kilinrax (Deacon)
on Sep 11, 2002 at 18:30 UTC ( #197028=perlquestion: print w/replies, xml ) Need Help??

kilinrax has asked for the wisdom of the Perl Monks concerning the following question:

Interfacing C++ and Perl seems to be a somewhat underdocumented subject; so far I have only managed to find the following:

  1. John Keiser's Gluing C++ And Perl Together
  2. Dean Roehrich's XS CookBooks
However, neither of these really touch on writing frontends to C++ libs, just standalone code.

If anyone can suggest a better forum for this, or source of documentation specific to what I'm trying to do, when please tell me. If not, and you have any experience of what I'm trying to do, read on ...

I've been comsistently encountering two errors - one of which occurs whenever I try and access an overloaded method, e.g:

MyModule.c:64: no matching function for call to `Mysettings::set (cons +t string *, const string *)' /usr/local/include/mysettings.h:66: candidates are: void Mysettings::s +et(const string &, const string &) /usr/local/include/mysettings.h:74: void Mysettings::s +et(const string &, const char *) /usr/local/include/mysettings.h:82: void Mysettings::s +et(const string &, int) /usr/local/include/mysettings.h:90: void Mysettings::s +et(const string &, double) /usr/local/include/mysettings.h:98: void Mysettings::s +et(const string &, bool)

The second occurring when accessing methods which return an object of a differing class to the owner of the method, e.g

MySet MyEnquire::get_set(first, maxitems)
Produces the error:
MyModule.c: In function `void XS_MyModule__Enquire_get_set(CV *)': MyModule.c:567: cannot convert `RETVAL' from type `MySet' to type `voi +d *

I've tried googling for '"cannot convert `RETVAL' from type `MySet"', which turns up absolutely no results; and '"no matching function for call to" XS', which turns up ones of little relevance.

Anyone know how to deal with these problems?

n.b. Before anyone suggests it, I have tried generating the XS code with SWIG, but had even less success with it.

Replies are listed 'Best First'.
Re: Writing XS frontends to C++ libraries.
by Zaxo (Archbishop) on Sep 11, 2002 at 19:34 UTC

    On your first question, C++ libraries mangle function names to attach an encoding of their agrument types. In that way, overloaded functions can be distinguished. Run the nm utility on a C++ lib to see examples. The c++filt does demangling.

    The MySet class or type is not known to Perl without a little work. You need to produce a typemap to show perl what it is. Perhaps including its header in your h2xs run will suffice.

    After Compline,

      I'm still confused as to why there should be an ambiguity about this section of the XS file:

      void MySettings::set(const string &key, const string &value)
      When the error produced by the code shows that we have what looks like a matching method:
      MyModule.c:64: no matching function for call to `Mysettings::set (cons +t string *, const string *)' /usr/local/include/mysettings.h:66: candidates are: void Mysettings::s +et(const string &, const string &)
      Does XS not distinguish well between pointers and references?

      MySet does have a typemap entry, of type O_OBJECT. I'll admit my understanding of typemaps is basic; this is just taken from the combining C++ and Perl link above.

        The code you posted should look like this:

        void MySettings::set(key, value) string * key string * value
        You're right about the problems with references. Don't try to use '&' in the XS prototype -- it definitely does not mean the same as C++. Also remember that in XS a const object is different from non-const, so you need two typemap definitions if you want to handle const objects.

        In general, you should use non-const pointers to interface C++ with perl -- eventually perl is going to stuff your object into an int, so it can't be bigger than a pointer. Pointers are conceptually easier to store, and const is going to be casted away anyways. Do your typemaps all use pointers?

        If you want to interface perl with methods that take const or reference arguments, the easiest way is to write the XS prototype with non-const pointers and then call the method yourself in the CODE: section. Here's how you'd call MySettings::set(const string &key, const string &value) from XS:

        void MySettings::set(key, value) string * key string * value CODE: THIS->set(*key, *value);
        Try perldoc perlxs for more on XS. If you stick to non-const pointers you'll be able to follow most of the examples designed for C.

Returning C++ objects from XS
by blssu (Pilgrim) on Sep 12, 2002 at 11:43 UTC

    Returning objects should still be done with pointers -- you must use C++ new to allocate all C++ objects you want to return to perl.

    Here's how to write the get_set example above. First you need to return a pointer, not an object:

    MySet * MyEnquire::get_set(first, maxitems)
    Then you need to add declarations for first and maxitems (I'm just guessing at what the types might be):
    MySet * MyEnquire::get_set(first, maxitems) MySetIterator * first int maxitems
    If MySetIterator is a template, I would typedef it to a simple name and use only the simple name in XS code (and the typemap). I'm not sure if XS needs that, but I use macros to help convert type names to Perl package names and the macros do need simple symbols.

    Finally, you need to glue the XS to the method call itself. If the get_set method returns an object, you need to make a copy. If it takes first as an object, you need to dereference the pointer:

    MySet * MyEnquire::get_set(first, maxitems) MySetIterator * first int maxitems CODE: RETVAL = new MySet (); *RETVAL = THIS->get_set(*first, maxitems); OUTPUT: RETVAL

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://197028]
Approved by John M. Dlugosz
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2022-01-19 01:20 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (54 votes). Check out past polls.