in reply to embedding Perl into C for Windows

Hi,

With the code you posted, I too get a crash on latest Strawberry Perl (5.18.2).
By inserting a couple of debug printf() statements, I found that it crashed when perl_parse() was executed. Same for you ?

However, there's a very similar example given in Strawberry Perl's perlembed (perldoc perlembed) documentation:
#include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; static void PerlPower(int a, int b) { dSP; /* initialize stack pointer */ ENTER; /* everything created after here */ SAVETMPS; /* ...is a temporary variable. */ PUSHMARK(SP); /* remember the stack pointer */ XPUSHs(sv_2mortal(newSViv(a))); /* push the base onto the stack */ XPUSHs(sv_2mortal(newSViv(b))); /* push the exponent onto stack */ PUTBACK; /* make local stack pointer global */ call_pv("expo", G_SCALAR); /* call the function */ SPAGAIN; /* refresh stack pointer */ /* pop the return value from stack */ printf ("%d to the %dth power is %d.\n", a, b, POPi); PUTBACK; FREETMPS; /* free that return value */ LEAVE; /* ...and the XPUSHed "mortal" args.*/ } int main (int argc, char **argv, char **env) { char *my_argv[] = { "", "power.pl" }; PERL_SYS_INIT3(&argc,&argv,&env); my_perl = perl_alloc(); perl_construct( my_perl ); perl_parse(my_perl, NULL, 2, my_argv, (char **)NULL); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_run(my_perl); PerlPower(3, 4); /*** Compute 3 ** 4 ***/ perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); }
With the file "power.pl" that contains:
sub expo { my ($a, $b) = @_; return $a ** $b; }
That example works fine, for me.
By working through the differences, you should be able to determine the reason(s) that one works, but the other doesn't.
(I'm assuming the version provided by the Strawberry docs works for you.)

Cheers,
Rob

Replies are listed 'Best First'.
Re^2: embedding Perl into C for Windows
by ccad (Novice) on Feb 12, 2014 at 07:24 UTC
    Hello Rob, this was the missing link:
    PERL_SYS_INIT3(&argc,&argv,&env); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; PERL_SYS_TERM();
    I added these three commands - now it works! Many, many thanks - great! Greeting from Germany Frank