Dearest Monks, I'm trying to create a small C program that uses Perl to work with some YAML (XS).

Everything works, except that I really need this to be statically linked so that I only have a single executable that packs the Perl libraries + the LibYAML XS libs. I need it that way for distribution purposes where no Perl or LibYAML can be installed (but other dependencies may be ok, like LibC, etc.).

Like a Golang freak colleague says, this would be "just like Go". He urges me to use Go instead, but I just can't believe a C-Perl compiled interpreter could not do the job just as well :)

Searching around in the we takes me to staticperl, which is a quite complicated beast, based on Perl 5.12 and not really what I'm looking for.

Here's a proof-of-concept program and the compilation line.

#include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; int main(int argc, char **argv, char **env) { PERL_SYS_INIT3(&argc,&argv,&env); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; char *embedding[] = { "", "-e", "0" }; my_perl = perl_alloc(); perl_construct(my_perl); perl_parse(my_perl, NULL, 3, embedding, NULL); perl_run(my_perl); eval_pv( "use YAML::XS;", TRUE ); perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); }

cc myprog.c -o myprog `perl -MExtUtils::Embed -e ccopts -e ldopts`

Note I'm not using the Dynaloader in this example as I assume, if correctly linked as a static binary, my binary would not need Perl's Dynaloader... right?


In reply to Compiling C program with a Static Perl by rodd

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.