Hello PerlMonks,
i want to embedd a perl interpreter into a C program. See below an example, very similar to the perl doc.
Now the problem is: The C program and the perl sub are running fine under LINUX. Compiling under Windows7 works fine, too, but it crashes on start. What am i doing wrong?
I used the latest Strawberry Perl und did the following:
perl -MExtUtils::Embed -e ccopts -e ldopts > cp1.bat
and modified this batch file by inserting gcc -o ccp2 ccp2.c (and the rest what was piped in). As i said, compiling under Windows was a breeze but on start the executable crashed.
/**********************************************
* C calls a Perl subroutine and pushes two
* double scalars.These two scalars are modified
* in the sub and popped.
* 7.2.2014 Frank
**********************************************/
#include <EXTERN.h>
#include <perl.h>
#include <stdio.h>
/*---------------------------------------------
* global variables
*--------------------------------------------*/
double x, y;
int icoun;
static PerlInterpreter *my_perl;
/*---------------------------------------------
* the C-"Assembly"routine
*--------------------------------------------*/
static void harry(double x, double y)
{
printf("in Harry vor call, x= %lf, y= %lf\n",x,y);
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVnv(x))); // PUSH one double "n"
XPUSHs(sv_2mortal(newSVnv(y))); // PUSH one double "n"
PUTBACK;
icoun= call_pv("points",G_ARRAY);
SPAGAIN;
y= POPn; // POP one double "n"
x= POPn; // POP one double "n"
printf("in Harry after call, icoun= %d, x= %lf, y= %lf\n",icoun,x,y);
PUTBACK;
FREETMPS;
LEAVE;
}
/*---------------------------------------------
* main
*--------------------------------------------*/
int main(int argc, char **argv, char **env)
{
char *my_argv[]= {"", "punkte2.pl"};
x= 3.1416;
y= 47.11;
printf("Controll of the entry values\n");
printf("X= %lf, Y= %lf\n",x,y);
/*=============================================
* start Perl Interpreter
*============================================*/
my_perl= perl_alloc();
perl_construct(my_perl);
perl_parse(my_perl,NULL, 2, my_argv, (char **)NULL);
perl_run(my_perl);
/*=============================================
* launch the C-"Assembly" routine
*============================================*/
harry(x,y);
/*=============================================
* ...and clean up
*============================================*/
perl_destruct(my_perl);
perl_free(my_perl);
}
#*********************************************
# the Subroutine points
# 7.2.2014 Frank
#*********************************************
sub points
{
my ($xup,$yup)= @_; # value,value
print "in der Subroutine\n"; # kleiner Test
print "XUP= $xup, YUP= $yup\n";
$xup *= 3;
$yup *= 4;
return $xup, $yup;
}
Has anyone an idea? Many thanks in advance!
Frank