Hi, I'm trying to write a XS routine to return a point to a c++ class to be
accessible from perl..

I'm new to XS, so i've been following:

http://www.johnkeiser.com/perl-xs-c++.html

and

http://search.cpan.org/~dmr/CookBookB-19960430/

However when I try make the xs generated code, i get the following compiler
error:

g++ -c  -I. -D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fno-strict-aliasing -pipe
-fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE
-D_FILE_OFFSET_BITS=64 -O2 -g   -DVERSION=\"0.01\" -DXS_VERSION=\"0.01\"
-fPIC "-I/usr/lib/perl/5.10/CORE"   MyPackage.c
MyPackage.c: In function ‘void XS_MyPackage_child(PerlInterpreter*, CV*)’:
MyPackage.c:168: error: ‘CLASS’ was not declared in this scope
make: *** [MyPackage.o] Error 1

I assume I'm doing something wrong in the child method, however i lifted
this code out of one of the CookBookB examples..

MyClass *
MyClass::child()
  CODE:
    RETVAL = (MyClass *)safemalloc( sizeof(MyClass) );
    *RETVAL = THIS->child();

  OUTPUT:
    RETVAL

  CLEANUP:
    safefree(RETVAL);

Any help would be much appreciated.

I have the following code:

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "ppport.h"

#ifdef __cplusplus
  extern "C" {
#endif
    #include "EXTERN.h"
    #include "perl.h"
    #include "XSUB.h"
#ifdef __cplusplus
  }
#endif

#undef list

#include <iostream>

class MyClass;

class MyClass {
  private:
    int _id;

  public:
    MyClass(int id) {
      _id = id;
    }

    ~MyClass() { }

    void operator =(const MyClass &other) {
      _id = other._id;
    }

    int id() {
      return _id;
    }

    MyClass child() {
      return MyClass(5);
    }
};

MODULE = MyPackage    PACKAGE = MyPackage

MyClass *
MyClass::new(int id)

void
MyClass::DESTROY()

MyClass *
MyClass::child()
  CODE:
    RETVAL = (MyClass *)safemalloc( sizeof(MyClass) );
    *RETVAL = THIS->child();

  OUTPUT:
    RETVAL

  CLEANUP:
    safefree(RETVAL);

With the Makefile.PL:

use 5.010001;
use ExtUtils::MakeMaker;

$CC = 'g++';
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
    NAME              => 'MyPackage',
    VERSION_FROM      => 'lib/MyPackage.pm', # finds $VERSION
    PREREQ_PM         => {}, # e.g., Module::Name => 1.1
    ($] >= 5.005 ?     ## Add these new keywords supported since 5.005
      (ABSTRACT_FROM  => 'lib/MyPackage.pm', # retrieve abstract from module
       AUTHOR         => 'James <james@>') : ()),
    LIBS              => [''], # e.g., '-lm'
    DEFINE            => '', # e.g., '-DHAVE_SOMETHING'
    INC               => '-I.', # e.g., '-I. -I/usr/include/other'
    CC                => $CC,
    LD                => '$(CC)',
    XSOPT             => '-C++',
    TYPEMAPS          => ['perlobject.map' ],
  # Un-comment this if you add C files to link with later:
    # OBJECT            => '$(O_FILES)', # link all the C files too
);

and the typemap:

TYPEMAP
MyClass *         O_OBJECT


Cheers James

Reply via email to