Perl 5.14源代码 - 示例程序失败

时间:2011-12-06 07:16:50

标签: c++ linux perl

Perl 5.14 source - 示例程序失败

我试图在Linux 64上执行以下程序,其中libperl.so使用5.14源代码构建 并且我在该位置中止了

Program terminated with signal 11, Segmentation fault.
#0  0x00002abdc0eb2656 in Perl_sv_2mortal () from ./libperl.so
(gdb) where
#0  0x00002abdc0eb2656 in Perl_sv_2mortal () from ./libperl.so
#1  0x00000000004010ed in PerlPower ()
#2  0x0000000000401335 in main ()
(gdb)

我的节目:

#include <EXTERN.h>
#include <perl.h>
#include <stdio.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();
}

power.pl包含以下声明

sub expo {
my ($a, $b) = @_;
return $a ** $b;
}

以上示例C和perl程序取自链接http://perldoc.perl.org/perlembed.html#Evaluating-a-Perl-statement-from-your-C-program

我使用以下编译器选项

编译器:

cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBUGGING -fno-strict-aliasing -pipe -Wdeclaration-after-statement -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
optimize='-O2 -fmessage-length=0 -Wall -D_FORTIFY_SOURCE=2 -g -Wall -pipe',
cppflags='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBUGGING -fno-strict-aliasing -pipe -Wdeclaration-after-statement'
ccversion='', gccversion='4.1.2 20070115 (prerelease) (SUSE Linux)', gccosandvers=''
intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
alignbytes=8, prototype=define 

请帮我解决问题?

2 个答案:

答案 0 :(得分:2)

我也得到了一个,我得到了win32等价Faulting application power.exe, version 0.0.0.0, faulting module perl514.dll, version 0.0.0.0, fault address 0x000c155f. 但我也收到了一条错误信息:)

$ power.exe
Can't open perl script "power.pl": No such file or directory

从embed复制power.pl后,它按预期工作

$ cat power.pl
sub expo {
    my ($a, $b) = @_;
    return $a ** $b;
}

$ power.exe
3 to the 4th power is 81.

答案 1 :(得分:0)

问题是在编译源代码时我没有指定新的5.14.2头文件位置。我将c和perl文件都复制到包含5.14.2新头文件的文件夹中,并使用以下选项进行编译解决了问题

g ++ -o test_perl test_perl.c -I。 -L。 -g perl -MExtUtils::Embed -e ccopts -e ldopts LD_LIBRARY_PATH =:LD_LIBRARY_PATH;导出LD_LIBRARY_PATH