I can use the perl runtime from a C application using the recipes written in the documentation, e.g. (trivial lines left out for brevity):
#include <EXTERN.h> #include <perl.h> #include "XSUB.h" 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 ); } int main( int argc, char **argv, char **env ) { PerlInterpreter *my_perl; PERL_SYS_INIT3( &argc, &argv, &env ); my_perl = perl_alloc(); 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; }
This works great, however this requires that the application program is linked against the perl shared library. I want to distribute this application with a custom perl shared library. Still not a problem, I link the application with --rpath=. and place the shared library next to the application, for example:
$ cd /tmp/foo $ ls myapp myperl.so $ ./myapp Hello, world!
Still fine. But I want to be able to call the application from arbitrary places, e.g.
$ cd $ ls /tmp/foo myapp myperl.so $ /tmp/foo/myapp /tmp/foo/myapp: error while loading shared libraries: myperl.so: canno +t open shared object file: No such file or directory
This is as it should be. When linking with --rpath=. the shared library must be in the current directory. What I would want is that the shared library is looked up there where the application is. I can use a wrapper script that sets LD_LIBRARY_PATH before calling the application but I'd rather want to solve this within the application itself. Fortunately, there is such a thing as dlopen:
int main( int argc, char **argv, char **env ) { /* find path to shared library */ /* open shared lib */ void *handle = dlopen(...); /* Get entry point for perl_alloc */ void* (*perl_alloc)(); perl_alloc = (void*())dlsym(handle, "perl_alloc") /* Call perl_alloc */ my_perl = (*perl_alloc)(); /* and so on */ /* ... */ }
This, too, works nice ... except for one detail: I cannot get the second argument to perl_parse (xs_init above, to set up the DynaLoader) right. Can anyone shed some light on how to get this working?

In reply to How to setup the DynaLoader in a dynamically loaded perl? by sciurius

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.