Error: Can't load '/home/dr/Abby/XS/Mytest/blib/arch/auto/Mytest/Mytest.so' for module Mytest: /home/dr/Abby/XS/Mytest/blib/arch/auto/Mytest/Mytest.so: undefined symbol: _ZN7MyClassD1Ev at /usr/lib/perl/5.14/DynaLoader.pm line 184.
####
#ifndef MyClass_H
#define MyClass_H
class MyClass {
public:
MyClass(int a, int b);
~MyClass();
int area();
private:
int length;
int width;
};
#endif
####
#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;
}
Makefile.PL
This I basically took from the Tutorial mentioned in the Into, and adjusted it to my changes.
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 "Position 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
';
}
####
#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()
####
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 module
AUTHOR => 'Dennis Rieber ') : ()),
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)
';
}
####
# Before `make install' is performed this script should be runnable with
# `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 so read
# its man page ( perldoc Test::More ) for help writing this test script.
####
Changes
Makefile.PL
MANIFEST
Mytest2.xs
ppport.h
README
t/Mytest2.t
lib/Mytest2.pm