use strict; use warnings; use IO::Socket; # without this line, no crash ! print "** hello from hello.pl **\n"; #### // perlLib.c #include #include EXTERN_C void xs_init (pTHX); PerlInterpreter *my_perl; void myPerlFunc() { char *largv[] = { "", "-f", "hello.pl", NULL }; int largc=3; my_perl = perl_alloc(); perl_construct(my_perl); perl_parse(my_perl, xs_init, largc, largv, environ); perl_run(my_perl); perl_destruct(my_perl); perl_free(my_perl); } #### // perlEmbedDynamic.c #include #include int main(int argc, char **argv) { void *handle; void (*theLibFunction)(); handle = dlopen ("libDynamicPerl.so", RTLD_LAZY); theLibFunction = dlsym(handle, "myPerlFunc"); (*theLibFunction)(); dlclose(handle); return(0); } #### # compTest.sh # generate and compile perxsi.c perl -MExtUtils::Embed -e xsinit -- -o perlxsi.c cc -Wall -fPIC -g -c perlxsi.c `perl -MExtUtils::Embed -e ccopts` # perlLib contains the function myPerlFunc which embeds and runs the interpreter cc -Wall -fPIC -g -c perlLib.c `perl -MExtUtils::Embed -e ccopts` # generate the dynamic libray cc -Wall -shared -Wl,-export-dynamic -o libDynamicPerl.so perlLib.o perlxsi.o `perl -MExtUtils::Embed -e ldopts` # linking of the library cc -Wall -L. -lDynamicPerl perlEmbedDynamic.c -o perlEmbedDynamicLinking # execute with linking, works fine ./perlEmbedDynamicLinking # dynamic loading of the library cc -rdynamic -o perlEmbedDynamicLoading perlEmbedDynamic.c -ldl # execute with dynamic loading, crashes ./perlEmbedDynamicLoading