Import a C function into a Perl program

I would like to import the C function that I wrote

#include <math.h>
#include <stdio.h>

double function (const double *restrict ARRAY1, const size_t ARRAY1_SIZE, const double *restrict ARRAY2, const size_t ARRAY2_SIZE) {//calculate a p-value based on an array
....
}
int main(){
....
}

in the perl script, I saw Inline :: C and XS, but I don’t see how to use them, I can’t work with examples, and I also need the lgamma function. The function accepts 2 arrays as input.

Can someone provide an example of how I can import this into a perl script while importing C math.h at the same time?

+4
source share
2 answers

The tricky part of this question is passing arrays of doubles from Perl to C.

, . Perl (AV* C) , . perl , , perlguts

use Inline 'C';
@a = (1,2,3,4,5);
@b = (19,42);
$x = c_function(\@a,\@b);
print "Result: $x\n";
__END__
__C__
#include <stdio.h>
#include <math.h>
double *AV_to_doubleptr(AV *av, int *len)
{
    *len = av_len(av) + 1;
    double *array = malloc(sizeof(double) * *len);
    int i;
    for (i=0; i<*len; i++)
        array[i] = SvNV( *av_fetch(av, i, 0) );
    return array;  /* returns length in len as side-effect */
}

double the_real_function(const double *x1, int n1, const double *x2, int n2)
{
    ...
}

double c_function(AV *av1, AV *av2)
{
    int n1, n2;
    double *x1 = AV_to_doubleptr(av1, &n1);
    double *x2 = AV_to_doubleptr(av2, &n2);
    double result = the_real_function(x1,n1, x2,n2);
    free(x2);
    free(x1);
    return result;
}
+5

Inline:: C math.h:

use warnings;
use strict;

use Inline 'C';

my $num = c_function(5, 5);

print "$num\n";

__END__
__C__

#include <math.h>
#include <stdio.h>

double c_function(int x, int y){
    return pow(x, y);
}

:

3125
+3

All Articles