After further discussion with tigger^ on IRC, and some tinkering, I came up with the following.

/* gcc -Wall -c perl-embed.c `perl -MExtUtils::Embed -e ccopts` gcc -Wall perl-embed.o `perl -MExtUtils::Embed -e ldopts` -o perl +-embed */ #include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; EXTERN_C void xs_init _((pTHX)); EXTERN_C void boot_DynaLoader _((pTHX_ CV* cv)); EXTERN_C void xs_init(pTHX) { dXSUB_SYS; newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__); } int main(int argc, char **argv, char **env) { char *embedding[] = { "", "-e" }; SV *my_sv; double (*my_symbol)(double, double); my_perl = perl_alloc(); perl_construct(my_perl); perl_parse(my_perl, xs_init, 3, embedding, (char **)NULL); perl_run(my_perl); my_sv = eval_pv( "use DynaLoader;" "my $h = DynaLoader::dl_load_file(\"libm.so\");" "DynaLoader::dl_find_symbol($h, \"pow\");", TRUE ); my_symbol = (void *)SvIV(my_sv); printf( "Symbol %p, (*my_symbol)(4,4) = %f\n", my_symbol, (*my_symbol)(4,4) ); perl_destruct(my_perl); perl_free(my_perl); return 0; }

Note, I'm using the standard C function pow() as a stand-in for the original PmmNodeToSv. The important part is that the library is loaded and the symbol retrieved using DynaLoader.

The problem with this solution is that it uses a specifically unspecified feature of DynaLoader. The documentation specifically states the symbol returned by dl_find_symbol() or dl_find_symbol_anywhere() should only be used in a call to dl_install_xsub(), you should not rely on it for anything else.

The alternative to this approach seems even less portable though, namely to find the library (auto/XML/LibXML.so) in @INC, dlopen() it, then fetch the symbol with dlsym(). At least with the DynaLoader method you get some level of abstraction between operating systems.

Are there any alternatives to these methods? How portable is using the symbol returned by DynaLoader, really? From my reading of the source (ext/DynaLoader/dl_*.xs) it seems like this method should work on every OS. Perhaps the documentation is simply leaving a window open for later changes.

update: removed extraneous include of dlfcn.h, a leftover from earlier attempts at a solution.


In reply to Re: Relocation error with an embedded perl interpreter by Somni
in thread Relocation error with an embedded perl interpreter by Anonymous Monk

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.