ekopalypse has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm looking for some advice on how to build an embedded perl interpreter on win7 x64 What I did so far is, cloned the perl github repo and built, tested and installed perl by doing the following in the win32 directory within a x64 Native Tools Command Prompt for VS 2017 Now, when trying to build an embedded perl with the same compiler VS2017 community and using the minimal sample
#include "pch.h" #include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; int main(int argc, char **argv, char **env) { my_perl = perl_alloc(); perl_construct(my_perl); perl_parse(my_perl, NULL, argc, argv, (char **)NULL); perl_run(my_perl); perl_destruct(my_perl); perl_free(my_perl); }
it builds the exe but crashes on perl_parse api function when executing it like this

embeddedperl test.pl.

Note, the lib directory as well as the perl.dll has been copied over to VS output directory from the previously created and installed perl interpreter. It looks like the issue is that it is looking for a path or perl module called MSWin32-x64-multi-thread like ...\EmbeddedPerl\x64\Debug\lib\MSWin32-x64-multi-thread. (revealed by procmon) but this hasn't been created. As I'm neither an experienced perl user nor c/c++ programmer, it is most likely that the issue raised from something stupid I did. I tried it with perl 5.30 as well as with the bleed branch from github but the result was the same. Similar code was working on linux ubuntu 19.04. Questions: Thank you Eko

Replies are listed 'Best First'.
Re: Embedded perl on windows x64
by syphilis (Archbishop) on Aug 01, 2019 at 02:18 UTC
    Note, the lib directory as well as the perl.dll has been copied over to VS output directory from the previously created and installed perl interpreter

    That seems a rather odd thing to do. Ensuring that the directory that houses the perl.dll is in the PATH is normally all that's needed.

    Is VS2017 community edition suitable for building a 64bit embedded perl interpreter?

    I would expect so, but I haven't tested.

    If so, are there some recommendations on how to do this?

    Yes, perlembed.

    If not, might using mingw toolchain create an embedded perl...

    Yes.

    However, I too strike issues with your C code. Not sure where you got it from, but the current perlembed docs present it slightly differently.
    Here's what I get (with mingw built perl-5.30.0) when I use the C code provided by the perl-5.30.0 perlembed docs:
    C:\_32\pscrpt\embed>type embed.c #include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; int main(int argc, char **argv, char **env) { PERL_SYS_INIT3(&argc,&argv,&env); my_perl = perl_alloc(); perl_construct(my_perl); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_parse(my_perl, NULL, argc, argv, (char **)NULL); perl_run(my_perl); perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); exit(EXIT_SUCCESS); } C:\_32\pscrpt\embed>perl -MExtUtils::Embed -e ccopts -s -O2 -DWIN32 -DWIN64 -DCONSERVATIVE -DPERL_TEXTMODE_SCRIPTS -DPER +L_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO -D__USE_MINGW_ANS +I_STDIO -fwrapv -fno-strict-aliasing -mms-bitfields -I"C:\_64\perl53 +0_810\lib\CORE" C:\_32\pscrpt\embed>perl -MExtUtils::Embed -e ldopts -s -L"C:\_64\perl530_810\lib\CORE" -L"C:\_64\gcc-mingw-810\mingw64\l +ib" "C:\_64\perl530_810\lib\CORE\libperl530.a" "C:\_64\gcc-mingw-810 +\mingw64\x86_64-w64-mingw32\lib\libmoldname.a" "C:\_64\gcc-mingw-810\ +mingw64\x86_64-w64-mingw32\lib\libkernel32.a" "C:\_64\gcc-mingw-810\m +ingw64\x86_64-w64-mingw32\lib\libuser32.a" "C:\_64\gcc-mingw-810\ming +w64\x86_64-w64-mingw32\lib\libgdi32.a" "C:\_64\gcc-mingw-810\mingw64\ +x86_64-w64-mingw32\lib\libwinspool.a" "C:\_64\gcc-mingw-810\mingw64\x +86_64-w64-mingw32\lib\libcomdlg32.a" "C:\_64\gcc-mingw-810\mingw64\x8 +6_64-w64-mingw32\lib\libadvapi32.a" "C:\_64\gcc-mingw-810\mingw64\x86 +_64-w64-mingw32\lib\libshell32.a" "C:\_64\gcc-mingw-810\mingw64\x86_6 +4-w64-mingw32\lib\libole32.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w6 +4-mingw32\lib\liboleaut32.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64 +-mingw32\lib\libnetapi32.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64- +mingw32\lib\libuuid.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64-mingw +32\lib\libws2_32.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64-mingw32\ +lib\libmpr.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64-mingw32\lib\li +bwinmm.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64-mingw32\lib\libver +sion.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64-mingw32\lib\libodbc3 +2.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64-mingw32\lib\libodbccp32 +.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64-mingw32\lib\libcomctl32. +a"
    Now create the executable (embed.exe):
    C:\_32\pscrpt\embed>gcc -o embed.exe embed.c -s -O2 -DWIN32 -DWIN64 -D +CONSERVATIVE -DPERL_TEXTMODE_SCRIPTS -DPERL_IMPLICIT_CONTEXT -DPERL_ +IMPLICIT_SYS -DUSE_PERLIO -D__USE_MINGW_ANSI_STDIO -fwrapv -fno-stric +t-aliasing -mms-bitfields -I"C:\_64\perl530_810\lib\CORE" -s -L"C: +\_64\perl530_810\lib\CORE" -L"C:\_64\gcc-mingw-810\mingw64\lib" "C:\ +_64\perl530_810\lib\CORE\libperl530.a" "C:\_64\gcc-mingw-810\mingw64\ +x86_64-w64-mingw32\lib\libmoldname.a" "C:\_64\gcc-mingw-810\mingw64\x +86_64-w64-mingw32\lib\libkernel32.a" "C:\_64\gcc-mingw-810\mingw64\x8 +6_64-w64-mingw32\lib\libuser32.a" "C:\_64\gcc-mingw-810\mingw64\x86_6 +4-w64-mingw32\lib\libgdi32.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w6 +4-mingw32\lib\libwinspool.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64 +-mingw32\lib\libcomdlg32.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64- +mingw32\lib\libadvapi32.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64-m +ingw32\lib\libshell32.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64-min +gw32\lib\libole32.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64-mingw32 +\lib\liboleaut32.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64-mingw32\ +lib\libnetapi32.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64-mingw32\l +ib\libuuid.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64-mingw32\lib\li +bws2_32.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64-mingw32\lib\libmp +r.a" "C:\_64\gcc-mingw-810\mingw64\x86_64-w64-mingw32\lib\libwinmm.a" + "C:\_64\gcc-mingw-810\mingw64\x86_64-w64-mingw32\lib\libversion.a" " +C:\_64\gcc-mingw-810\mingw64\x86_64-w64-mingw32\lib\libodbc32.a" "C:\ +_64\gcc-mingw-810\mingw64\x86_64-w64-mingw32\lib\libodbccp32.a" "C:\_ +64\gcc-mingw-810\mingw64\x86_64-w64-mingw32\lib\libcomctl32.a"
    Now test it:
    C:\_32\pscrpt\embed>embed -le "print 'hello';" hello C:\_32\pscrpt\embed>type hello.pl use strict; use warnings; print "hello\n"; C:\_32\pscrpt\embed>embed hello.pl hello
    So it all seems to be working as advertised once your C code is modified correctly.

    Cheers,
    Rob
      Hi Rob,
      thank you, I'm baffled, because I thought this was from perlembed but the link you provided
      proves I'm wrong, but still 100% sure, that the posted code runs on ubuntu 19.04, strange.

      The code change, partially, did it.
      Now an exception is raised at perl_destruct but let me first check the docs once again,
      maybe I'm still missing something.
      And thanks for the mingw example as well.
      I keep this thread updated.

      Thank you
      Eko
        ... the posted code runs on ubuntu 19.04

        Yes - the C code you posted also works fine for me on Ubuntu-18.04, perl-5.30.0.
        Apparently different operating systems can have different requirements.

        Also bear in mind that requirements across different versions of perl might change.
        The perlembed documentation that I linked to earlier is for perl-5.30.0.
        It might be more pertinent to check the perlembed docs by running perldoc perlembed, as that will give you the documentation for the version of perl that you have installed.

        Cheers,
        Rob
Re: Embedded perl on windows x64
by bliako (Abbot) on Aug 01, 2019 at 01:16 UTC

    I am not acquainted with that OS but I had been bitten before by missing debug libraries. Is there anywhere in the Makefile a "Release" target (as opposed to "Debug") so that you make release?

      Hello,

      thanks for the answer.
      My understanding is that it always creates a release build unless you specify the CFG=Debug flag.
      Is this what you are asking for?
      But it looks like my problem is/was that I used either an outdated or wrong perl documentation,
      which I'm going to revisit again. :-)

      Thank you
      Eko