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

In reply to embedding Perl into C for Windows by ccad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.