Perl XS unused variable 'Perl___notused' warnings
Asked Answered
P

1

8

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

Pedigo answered 6/11, 2017 at 21:6 Comment(8)
These kind of compiler warnings get significantly better (i.e. often disappear on their own) when you use a newer perl. Compatibility with C++ improves as well. If by any means possible, consider installing a recent release, e.g. via perlbrew. Perl 5.10.1 is over 8 years old.Gould
Unfortunately I only have what is available for Perl out of the box on redhat 6 :(Pedigo
Can you not install a later version of perl? Surely you should have access to the internet or some local yum repository?Boggle
@GerhardBarnard You're assuming admin privileges or anyway that the perl interpreter can be replaced (or used in a local installation) without any side effectsSweeten
@Sweeten I am not assuming anything, but any environment has root privileges or a team that has root privileges that can assist. Perl interpreters does not have to be replaced, I run numerous versions on a single redhat instance for different purposes.Boggle
@GerhardBarnard, replacing the current version of Perl is out of the question. Installing a new version in addition to what we have may be a possibility, but I'm not sure all of the implications it may have and there would definitely be concerns that it could impact other things. Would any XS Perl modules I write and compile with the new version even be usable by the older version of Perl?Pedigo
@tjwrona1992 No, XS modules can only be used with the Perl version they were compiled with. The Perl API gets new additions with each version. While the ppport.h can provide some backcompat, it is not usually enough that you could develop on a new perl but run on an older perl. Perlbrew makes it very easy to install multiple Perl versions side by side without admin privileges. The suggestion is that you find a way to install current perls both in your production and development environments.Gould
@GerhardBarnard, All access to the internet to install any software on the server is also restricted to admin only so I don't have the ability to install perlbrew even if I wanted to. And even if I had it, none of the commands to install anything would work since I can't reach out to the internet to get any updates. :(Pedigo
P
1

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.

Pedigo answered 24/1, 2018 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.