in reply to How to setup the DynaLoader in a dynamically loaded perl?
On second glance, you did not dlsym'ed boot_DynaLoader() that's why xs_init() had trouble.
This is a complete, compilable and runnable example:
/* author: bliako date: 05/05/2020 for: https://perlmonks.org/?node_id=11116476 compile with: $(perl -MConfig -e 'print $Config{cc}') a.c $(perl -MExtUtils::Embed - +e ccopts -e ldopts) -o aaa -ldl run with: ./aaa -e 'print "hello there, I am a tiny wee Perl!!\n"' */ #include <EXTERN.h> #include <perl.h> #include "XSUB.h" #include <dlfcn.h> // void boot_DynaLoader (pTHX_ CV* cv); /* this declares a function pointer as opposed to an external function + above */ void (*boot_DynaLoader)(pTHX_ CV* cv); void xs_init(pTHX) { static const char file[] = __FILE__; dXSUB_SYS; PERL_UNUSED_CONTEXT; newXS( "DynaLoader::boot_DynaLoader", boot_DynaLoader, file ); } static PerlInterpreter *my_perl; int main( int argc, char **argv, char **env ) { /* find path to shared library */ /* open shared lib */ void *handle = dlopen("/usr/lib64/libperl.so.5.28.2", RTLD_NOW); /* Get entry point for perl_alloc */ void* (*perl_alloc)(); perl_alloc = (void *(*)() )dlsym(handle, "perl_alloc"); /* Get all the functions you will be using from the library, t +his one is obvious */ boot_DynaLoader = (void (*)(pTHX_ CV* cv) )dlsym(handle, "boot_Dyn +aLoader"); /* Call perl_alloc */ my_perl = perl_alloc(); /* and so on */ perl_construct(my_perl); perl_parse( my_perl, xs_init, argc, argv, env ); int result = perl_run(my_perl); perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); return result; }
Edit: added a typecast to boot_DynaLoader = (void (*)(pTHX_ CV* cv) )dlsym(handle, "boot_DynaLoader");
bw, bliako
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to setup the DynaLoader in a dynamically loaded perl?
by sciurius (Beadle) on May 05, 2020 at 20:00 UTC | |
by bliako (Abbot) on May 05, 2020 at 21:06 UTC |