I am learning how to call C++ code from Perl and to start I am trying to create a basic C++ object from a Perl script.
To do this, I started by executing the h2xs
command:
h2xs -A -nMyClass
Then I added the following two arguments to the generated Makefile.PL
to use the g++
compiler.
CC => 'g++',
LD => 'g++',
I created my simple class in the .xs
file and wrote the XS code to map it with Perl
MyClass.xs
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#ifdef __cplusplus
}
#endif
class MyClass {
public:
MyClass(int value) {
value_ = value;
}
~MyClass() {}
int value() { return value_; }
void set_value(int value) {
value_ = value;
}
private:
int value_;
};
MODULE = MyClass PACKAGE = MyClass
MyClass *
MyClass::new(int value)
void
MyClass::DESTROY()
int
MyClass::value()
void
MyClass::set_value(int value)
Then I created the typemap file to map the new type to Perl.
typemap
TYPEMAP
MyClass * 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 blessed SV reference\" );
XSRETURN_UNDEF;
}
Finally I created a simple test.
t/MyClass.t
use Test::More tests => 2;
BEGIN { use_ok('MyClass') };
my $obj = MyClass->new(1);
ok($obj->isa('MyClass'), 'MyClass object constructed');
I then successfully built the code and ran the tests.
perl Makefile.PL
make
make test
Although everything works fine, I get some warnings with the build:
MyClass.c: In function 'void XS_MyClass_new(PerlInterpreter*, CV*)':
MyClass.c:95: warning: unused variable 'Perl___notused'
MyClass.c: In function 'void XS_MyClass_DESTROY(PerlInterpreter*, CV*)':
MyClass.c:119: warning: unused variable 'Perl___notused'
MyClass.c: In function 'void XS_MyClass_value(PerlInterpreter*, CV*)':
MyClass.c:145: warning: unused variable 'Perl___notused'
MyClass.c: In function 'void XS_MyClass_set_value(PerlInterpreter*, CV*)':
MyClass.c:174: warning: unused variable 'Perl___notused'
MyClass.c: In function 'void boot_MyClass(PerlInterpreter*, CV*)':
MyClass.c:203: warning: unused variable 'Perl___notused'
I searched all over trying to find the cause for these warnings, and can't figure out what is going on. All of the warnings seem come from the same repeated section in the code that occurs at the beginning of every function definition.
inside MyClass.c
XS(XS_MyClass_new); /* prototype to pass -Wmissing-prototypes */
XS(XS_MyClass_new)
{
#ifdef dVAR
dVAR; dXSARGS; // <-- warning occurs here
#else
dXSARGS;
#endif
// function body continues...
Can someone please tell me the root cause of these warnings?
I am using Perl v5.10.1 and g++ version 4.4.7
The solution is to use a newer version of Perl. v5.10.1 is very outdated and these problems don't occur in later versions.
I am posting an answer to my own question because it was answered in the comments, but an official answer was never posted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With