On Jul 25, 8:35 pm, [EMAIL PROTECTED] (Rajnikant)
wrote:
> Hello All,
>
> I'm trying to call C routine from Perl using XS but some how my 'make test'
> is failing. Following are the steps I did :
>
> file: hypotenuse.h
>       double hypotenuse(double x, double y); /* Func Declaration */
> file: hypotenuse.c
>       /* Func Definition */
>      double hypotenuse(double x, double y)
>      {
>            return sqrt(x*x + y*y);
>      }
>

I would probably just create a Makefile.PL that looks like:

----------------------------------
use ExtUtils::MakeMaker;
my %options = (
  'NAME' => 'Geometry',
  'VERSION' => '0.01'
);
WriteMakefile(%options);

# Remove the Makefile dependency. Causes problems on a few systems.
sub MY::makefile { '' }
----------------------------------

a Geometry.pm that looks like:

----------------------------------
package Geometry;
use strict;

require Exporter;
*import = \&Exporter::import;
require DynaLoader;

$Geometry::VERSION = '0.01';

DynaLoader::bootstrap Geometry $Geometry::VERSION;

@Geometry::EXPORT = ();
@Geometry::EXPORT_OK = ('hypotenuse');

sub dl_load_flags {0} # Prevent DynaLoader from complaining and
croaking

1;
----------------------------------

a Geometry.xs that looks like:

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

double hypotenuse(double x, double y) {
           return sqrt(x*x + y*y);
}
MODULE = Geometry       PACKAGE = Geometry

PROTOTYPES: DISABLE


double
hypotenuse (x, y)
        double  x
        double  y

----------------------------------

and a test.pl that looks like:

----------------------------------
use warnings;
use strict;
use Geometry 'hypotenuse';

print "1..1\n";

if(hypotenuse(5, 12) == 13) {print "ok 1\n"}
else {print "not ok 1\n"}
----------------------------------

I used InlineX::C2XS (needs Inline::C) to autogenerate the first 3 of
those 4 files - though there's some very minor hand editing that went
on as well wrt the Makefile.PL and Geometry.pm.
'test.pl' was written entirely by hand.
The source file that InlineX::C2XS used (./src/Geometry.c) looked
like:

----------------------------------
double hypotenuse(double x, double y) {
           return sqrt(x*x + y*y);
}
----------------------------------


Cheers,
Rob


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


  • XS help Rajnikant
    • Re: XS help sisyphus

Reply via email to