Intro

I'm trying to create a PerlXS-Modul, that uses a C++ Class. Before diving into the complex matter, I wanted to create a little Test, to see how the basics work, since this is my first time using PerlXS. Luckily I tumbled upon this http://www.perlmonks.org/?node_id=853194 thread, which is a pretty awesome piece of knowledge. So I started to create my own litte Testclass, according to the example in the tutorial.

The Problem

While 'make' works without errors, but the include ( make test etc.) of the modul into a perl script fails with the following error:
Error: Can't load '/home/dr/Abby/XS/Mytest/blib/arch/auto/Mytest/Myte +st.so' for module Mytest: /home/dr/Abby/XS/Mytest/blib/arch/auto/Myte +st/Mytest.so: undefined symbol: _ZN7MyClassD1Ev at /usr/lib/perl/5.14 +/DynaLoader.pm line 184.
I tried to as close a possibly with the tutorial, but something is off, and I don't know what. Has anyone seen something similar? Any hints?

Code

I created the folder structure using h2xs -A -n Mytest In there I created a folder, called 'myclass', where the code for my C++ Class

C++ Part

myclass.h
#ifndef MyClass_H #define MyClass_H class MyClass { public: MyClass(int a, int b); ~MyClass(); int area(); private: int length; int width; }; #endif
myclass.cpp
#include "myclass.h" using namespace std; MyClass::MyClass (int length, int width) { this->length = length; this->width = width; } int MyClass::area() { return this->length*this->width; } </class> Makefile.PL This I basically took from the Tutorial mentioned in the Into, and adj +usted it to my changes. <code> use ExtUtils::MakeMaker; my ( $CC, $Verbose ) = ( 'g++', 1 ); WriteMakefile( NAME => 'Mytest::myclass', SKIP => [qw(all static static_lib dynamic dynamic_lib)], clean => {'FILES' => 'libmyclass$(LIB_EXT) unitTest'}, CC => $CC, # Our CPP compiler LD => '$(CC)', CCFLAGS => '-fPIC', # this instructs the CPP compiler to use "Posi +tion independence", basically a shared library, more details here # http://www.fpx.de/fp/Software/tcl-c++/tc ++l-c++.html ); sub MY::top_targets { ' all :: static pure_all :: static static :: libmyclass$(LIB_EXT) libmyclass$(LIB_EXT): $(O_FILES) $(AR) cr libmyclass$(LIB_EXT) $(O_FILES) $(RANLIB) libmyclass$(LIB_EXT) bin: $(O_FILES) $(LD) $(O_FILES) -o unitTest '; }
To test all these I wrote a litte unitTest, which worked fine. So I can safely assume, that my problem is not within the C++ part.

XS-Part and Makefile.PL

Mytest.xs
#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" #include "myclass/myclass.h" MODULE = Mytest PACKAGE = Mytest MyClass* MyClass::new(int a, int b) int MyClass::area() void MyClass::DESTROY()
typemap  MyClass *      T_PTROBJ Makefile.PL
use 5.014002; use ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. my $CC = 'g++'; WriteMakefile( NAME => 'Mytest', VERSION_FROM => 'lib/Mytest.pm', # finds $VERSION PREREQ_PM => {}, # e.g., Module::Name => 1.1 CC => $CC, # Our CPP compiler LD => '$(CC)', ($] >= 5.005 ? ## Add these new keywords supported since 5.005 (ABSTRACT_FROM => 'lib/Mytest.pm', # retrieve abstract from mod +ule AUTHOR => 'Dennis Rieber <dr@>') : ()), LIBS => [''], # e.g., '-lm' DEFINE => '', # e.g., '-DHAVE_SOMETHING' INC => '-I.', # e.g., '-I. -I/usr/include/other' # Un-comment this if you add C files to link with later: # OBJECT => '$(O_FILES)', # link all the C files too MYEXTLIB => 'myclass/libmyclass$(LIB_EXT)', # TYPEMAPS => ['perlobject.map' ], ); sub MY::postamble { ' $(MYEXTLIB): myclass/Makefile cd myclass && $(MAKE) $(PASSTHRU) '; }
t/Mytest.t
# Before `make install' is performed this script should be runnable wi +th # `make test'. After `make install' it should work as `perl Mytest.t' ######################### # change 'tests => 1' to 'tests => last_test_to_print'; use strict; use warnings; use Test::More tests => 2; BEGIN { use_ok('Mytest') }; my $class = new Mytest::MyClass(2,3); is($class->area(), 6, 'area_ok') ######################### # Insert your test code below, the Test::More module is use()ed here s +o read # its man page ( perldoc Test::More ) for help writing this test scrip +t.
MANIFEST
Changes Makefile.PL MANIFEST Mytest2.xs ppport.h README t/Mytest2.t lib/Mytest2.pm
This should be all the relevant code, or did I miss something?

In reply to Unable to use created PerlXS Modul ( using c++ ) by lamchob

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.