Hi. My question concerns how one defines in XS that one has a C++ class. The only docs I can find on the subject of using XS with C++ is in the XS reference manual so I am trying to run their example but it is failing on "make" and the error indicates that the compiler is not recognizing the language as C++, so it doesn't recognize the class. This would make sense since it is compiling a .c file, namely myclass.c. I am confused. No matter what I do, it seems that xsubpp generates a .c file and never a .cc file. But if this is the case, then how will it ever recognize class declarations? Or is it saying that I have not supplied a class definition? What am I missing? Here's the output of "make":
cc -c -O -DVERSION=\"0.01\" -DXS_VERSION=\"0.01\" -Kpic -I/usr/ +local/lib/ perl5/sun4-solaris/5.00405/CORE myclass.c "/home/judyf/perlwork/myclass/myclass.h", line 9: undefined or not a t +ype: class "/home/judyf/perlwork/myclass/myclass.h", line 9: syntax error before +or at: col or "/home/judyf/perlwork/myclass/myclass.h", line 9: undefined or not a t +ype: color "/home/judyf/perlwork/myclass/myclass.h", line 15: syntax error before + or at: / "/home/judyf/perlwork/myclass/myclass.h", line 23: syntax error before + or at: in t "/home/judyf/perlwork/myclass/myclass.h", line 24: cannot recover from + previous errors cc: acomp failed for myclass.c *** Error code 2 make: Fatal error: Command failed for target `myclass.o'
Here's the class declaration from the myclass.h file:
class color { public: color(); ~color(); int blue(); int set_blue( int ); private: int c_blue; };
Here's the relevant .xs code:
int color::blue() void color::set_blue( val ) int val
My instinct would be, in the myclass.xs file, to create a new package within the one module (called "color") but based on the reference manual doc, I refrained from doing this and kept the class method declaration under the one package (with same name as module). hence, in myclass.xs:
MODULE = myclass PACKAGE = myclass
Here's the relevant portion of typemap file:
color * O_OBJECT OUTPUT # The Perl object is blessed into 'CLASS', which should be a # char* having the name of the package for the blessing. O_OBJECT sv_setref_pv( $arg, CLASS, (void*)$var ); INPUT O_OBJECT if( sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG) ) $var = ($type)SvIV((SV*)SvRV( $arg )); else{ warn( \"${Package}::$func_name() -- $var is not a bles +sed SV ref erence\" ); XSRETURN_UNDEF; }
Here's Makefile.PL (where I set 'XSOPT' to tell it I'm using C++):
use ExtUtils::MakeMaker; WriteMakefile( 'NAME' => 'myclass', 'VERSION_FROM' => 'myclass.pm', # finds $VERSION 'LIBS' => [''], # e.g., '-lm' 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING' 'INC' => '', # e.g., '-I/usr/include/other' 'XSOPT' => '-C++', );
Thanks for your help.

In reply to using XS with C++ by juda

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.