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

Ok, this is a cross-posting from StackOverflow http://stackoverflow.com/questions/27058788/attempt-to-embed-perl-in-c-on-windows . I posted it there first because I thought it is more C than Perl question. However I did not get any answer and so I'd like to try it again.

I am trying to run the following example from http://perl-node-interface.blogspot.de/2011/03/deploy-perl-application-on-windows.html (is not accepted as link) with a simple "Hello.pl" (just prints "Hello" to STDOUT).

It fails. The .exe file is created but does not produce any output.

Probably this is my basic misunderstanding. Could you please point me in the right direction? Btw. the "lib folder containing all dependencies" in the project folder is empty since there are no modules in the "hello.pl". Is this a correct assumption?

Thank you very much!

The hello.c file:

#include <EXTERN.h> #include <perl.h> EXTERN_C void xs_init (pTHX); EXTERN_C void boot_DynaLoader (pTHX_ CV* cv); EXTERN_C void boot_Win32CORE (pTHX_ CV* cv); EXTERN_C void xs_init(pTHX) { char *file = __FILE__; dXSUB_SYS; /* DynaLoader is a special case */ newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); newXS("Win32CORE::bootstrap", boot_Win32CORE, file); } static PerlInterpreter *my_perl; /*** The Perl interpreter ***/ int main(int argc, char **argv, char **env) { argv[1] = "-Ilib"; argv[2] = "hello.pl"; 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(); }

The perl file to build the compiler command:

#!/perl use strict; use warnings FATAL => qw(all); use ExtUtils::Embed; print "\nBuilding Hello\n"; my $gcc_cmd = join( ' ' , 'C:\Perl_516_portable\c\bin\gcc -Wall -mwind +ows -o K:\Scripts\Embed\Hello_3\hello K:\Scripts\Embed\Hello_3\hello. +c', &ccopts, &ldopts ); print STDOUT $gcc_cmd , "\n"; system( $gcc_cmd );

The output:

---------------------------------------------- Perl executable: C:\Perl_516_portable\perl\bin\perl.exe Perl version : 5.16.3 / MSWin32-x86-multi-thread C:\Perl_516_portable>perl K:\Scripts\Embed\Hello_3\building_3.pl Building Hello C:\Perl_516_portable\c\bin\gcc -Wall -mwindows -o K:\Scripts\Embed\He +llo_3\hello K:\Scripts\Embed\Hello_3\hello.c -s -O2 -DWIN32 -DPERL +_TEXTMODE_SCRIPTS -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -fno-st +rict-aliasing -mms-bitfields -I"C:\Perl_516_portable\perl\lib\CORE" + -s -L"C:\Perl_516_portable\perl\lib\CORE" -L"C:\Perl_516_portable\ +c\lib" C:\Perl_516_portable\perl\lib\CORE\libperl516.a C:\Perl_516_p +ortable\c\i686-w64-mingw32\lib\libmoldname.a C:Perl_516_portable\c\i6 +86-w64-mingw32\lib\libkernel32.a C:\Perl_516_portable\c\i686-w64-ming +w32\lib\libuser32.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\libgd +i32.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\libwinspool.a C:\Pe +rl_516_portable\c\i686-w64-mingw32\lib\libcomdlg32.a C:\Perl_516_port +able\c\i686-w64-mingw32\lib\libadvapi32.a C:\Perl_516_portable\c\i686 +-w64-mingw32\lib\libshell32.a C:\Perl_516_portable\c\i686-w64-mingw32 +\lib\libole32.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\liboleaut +32.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\libnetapi32.a C:\Per +l_516_portable\c\i686-w64-mingw32\lib\libuuid.a C:\Perl_516_portable\ +c\i686-w64-mingw32\lib\libws2_32.a C:Perl_516_portable\c\i686-w64-min +gw32\lib\libmpr.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\libwinm +m.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\libversion.a C:\Perl_ +516_portable\c\i686-w64-mingw32\lib\libodbc32.a C:\Perl_516_portable\ +c\i686-w64-mingw32\lib\libodbccp32.a C:\Perl_516_portable\c\i686-w64- +mingw32\lib\libcomctl32.a In file included from C:\Perl_516_portable\perl\lib\CORE/sys/socket.h: +180:0, from C:\Perl_516_portable\perl\lib\CORE/win32.h:356, from C:\Perl_516_portable\perl\lib\CORE/win32thread.h:4, from C:\Perl_516_portable\perl\lib\CORE/perl.h:2834, from K:\Scripts\Embed\Hello_3\hello.c:2: C:\Perl_516_portable\perl\lib\CORE/win32.h:361:26: warning: "/*" withi +n comment [-Wcomment] C:\Perl_516_portable\perl\lib\CORE/win32.h:362:33: warning: "/*" withi +n comment [-Wcomment] In file included from C:\Perl_516_portable\perl\lib\CORE/win32thread.h +:4:0, from C:\Perl_516_portable\perl\lib\CORE/perl.h:2834, from K:\Scripts\Embed\Hello_3\hello.c:2: C:\Perl_516_portable\perl\lib\CORE/win32.h:361:26: warning: "/*" withi +n comment [-Wcomment] C:\Perl_516_portable\perl\lib\CORE/win32.h:362:33: warning: "/*" withi +n comment [-Wcomment] K:\Scripts\Embed\Hello_3\hello.c: In function 'main': K:\Scripts\Embed\Hello_3\hello.c:37:1: warning: control reaches end of + non-void function [-Wreturn-type]

Replies are listed 'Best First'.
Re: Attempt to embed Perl in C on Windows
by BrowserUk (Patriarch) on Dec 21, 2014 at 20:48 UTC

    Strangely, your code built with MSVC and perl5.10 builds clean and runs perfectly:

    C:\test>cl /W3 hello.c -DWIN32 -I"C:\Perl64\lib\CORE" C:\Perl64\lib\C +ORE\perl510.lib Microsoft (R) C/C++ Optimizing Compiler Version 15.00.21022.08 for x64 Copyright (C) Microsoft Corporation. All rights reserved. hello.c Microsoft (R) Incremental Linker Version 9.00.21022.08 Copyright (C) Microsoft Corporation. All rights reserved. /out:hello.exe hello.obj C:\Perl64\lib\CORE\perl510.lib C:\test>.\hello.exe hello.pl Hello from MSWin32 5.010001 C:\test>type hello.pl #! perl -slw use strict; print "Hello from $^O $]\n"; C:\test>

    Built with gcc and 5.18 it builds clean(ish) and runs without crashing, but simply does nothing:

    C:\test>gcc_bld_hello.c.pl18 Building Hello gcc -Wall -mwindows -o hello hello.c -s -O2 -DWIN32 -DWIN64 -DCONSER +VATIVE -DPERL_TEXTMODE_SCRIPTS -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLIC +IT_SYS -DUSE_PERLIO -fno-strict-aliasing -mms-bitfields -I"C:\Perl5. +18\perl\lib\CORE" -s -L"C:\Perl5.18\perl\lib\CORE" -L"C:\Perl5.18\ +c\lib" C:\Perl5.18\perl\lib\CORE\libperl518.a C:\Perl5.18\c\x86_64-w +64-mingw32\lib\libmoldname.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\lib +kernel32.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\libuser32.a C:\Perl5. +18\c\x86_64-w64-mingw32\lib\libgdi32.a C:\Perl5.18\c\x86_64-w64-mingw +32\lib\libwinspool.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\libcomdlg32 +.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\libadvapi32.a C:\Perl5.18\c\x +86_64-w64-mingw32\lib\libshell32.a C:\Perl5.18\c\x86_64-w64-mingw32\l +ib\libole32.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\liboleaut32.a C:\P +erl5.18\c\x86_64-w64-mingw32\lib\libnetapi32.a C:\Perl5.18\c\x86_64-w +64-mingw32\lib\libuuid.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\libws2_ +32.a C:\Perl5.18\c\x86_64-w64-min gw32\lib\libmpr.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\libwinmm.a C:\P +erl5.18\c\x86_64-w64-mingw32\lib\libversion.a C:\Perl5.18\c\x86_64-w6 +4-mingw32\lib\libodbc32.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\libodb +ccp32.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\libcomctl32.a In file included from C:\Perl5.18\perl\lib\CORE/sys/socket.h:180:0, from C:\Perl5.18\perl\lib\CORE/win32.h:386, from C:\Perl5.18\perl\lib\CORE/win32thread.h:4, from C:\Perl5.18\perl\lib\CORE/perl.h:2869, from hello.c:2: C:\Perl5.18\perl\lib\CORE/win32.h:391:26: warning: "/*" within comment + [-Wcomment] C:\Perl5.18\perl\lib\CORE/win32.h:392:33: warning: "/*" within comment + [-Wcomment] In file included from C:\Perl5.18\perl\lib\CORE/win32thread.h:4:0, from C:\Perl5.18\perl\lib\CORE/perl.h:2869, from hello.c:2: C:\Perl5.18\perl\lib\CORE/win32.h:391:26: warning: "/*" within comment + [-Wcomment] C:\Perl5.18\perl\lib\CORE/win32.h:392:33: warning: "/*" within comment + [-Wcomment] C:\test>.\hello.exe hello.pl18 C:\test>type hello.pl18 #! perl -slw use strict; print "Hello from $^O $]\n"; C:\test>gcc_bld_hello.c.pl18

    And oddly, if I add a printf() at the top of the code:

    #include <EXTERN.h> #include <perl.h> EXTERN_C void xs_init (pTHX); EXTERN_C void boot_DynaLoader (pTHX_ CV* cv); EXTERN_C void boot_Win32CORE (pTHX_ CV* cv); EXTERN_C void xs_init(pTHX) { char *file = __FILE__; dXSUB_SYS; /* DynaLoader is a special case */ newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); newXS("Win32CORE::bootstrap", boot_Win32CORE, file); } static PerlInterpreter *my_perl; /*** The Perl interpreter ***/ int main(int argc, char **argv, char **env) { printf( "So far, so good!\n" ); 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(); return 0; }

    It still outputs nothing at all:

    C:\test>gcc_bld_hello.c.pl18 Building Hello gcc -Wall -mwindows -o hello hello.c -s -O2 -DWIN32 -DWIN64 -DCONSER +VATIVE -DPERL_TEXTMODE_SCRIPTS -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLIC +IT_SYS -DUSE_PERLIO -fno-strict-aliasing -mms-bitfields -I"C:\Perl5. +18\perl\lib\CORE" -s -L"C:\Perl5.18\perl\lib\CORE" -L"C:\Perl5.18\ +c\lib" C:\Perl5.18\perl\lib\CORE\libperl518.a C:\Perl5.18\c\x86_64-w +64-mingw32\lib\libmoldname.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\lib +kernel32.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\libuser32.a C:\Perl5. +18\c\x86_64-w64-mingw32\lib\libgdi32.a C:\Perl5.18\c\x86_64-w64-mingw +32\lib\libwinspool.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\libcomdlg32 +.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\libadvapi32.a C:\Perl5.18\c\x +86_64-w64-mingw32\lib\libshell32.a C:\Perl5.18\c\x86_64-w64-mingw32\l +ib\libole32.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\liboleaut32.a C:\P +erl5.18\c\x86_64-w64-mingw32\lib\libnetapi32.a C:\Perl5.18\c\x86_64-w +64-mingw32\lib\libuuid.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\libws2_ +32.a C:\Perl5.18\c\x86_64-w64-min gw32\lib\libmpr.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\libwinmm.a C:\P +erl5.18\c\x86_64-w64-mingw32\lib\libversion.a C:\Perl5.18\c\x86_64-w6 +4-mingw32\lib\libodbc32.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\libodb +ccp32.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\libcomctl32.a In file included from C:\Perl5.18\perl\lib\CORE/sys/socket.h:180:0, from C:\Perl5.18\perl\lib\CORE/win32.h:386, from C:\Perl5.18\perl\lib\CORE/win32thread.h:4, from C:\Perl5.18\perl\lib\CORE/perl.h:2869, from hello.c:2: C:\Perl5.18\perl\lib\CORE/win32.h:391:26: warning: "/*" within comment + [-Wcomment] C:\Perl5.18\perl\lib\CORE/win32.h:392:33: warning: "/*" within comment + [-Wcomment] In file included from C:\Perl5.18\perl\lib\CORE/win32thread.h:4:0, from C:\Perl5.18\perl\lib\CORE/perl.h:2869, from hello.c:2: C:\Perl5.18\perl\lib\CORE/win32.h:391:26: warning: "/*" within comment + [-Wcomment] C:\Perl5.18\perl\lib\CORE/win32.h:392:33: warning: "/*" within comment + [-Wcomment] C:\test>.\hello.exe hello.pl18 C:\test>

    I can't even begin to work out why that is. (I've yet to earn my Master's in -reading_stupidly_long_options -and_working_out_what_the_fcuk_they_are_meant_to_do. :)


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Perl usually hooks libc stuff like printf and malloc on win32 to make very old thread-unsafe XS code, thread-safe (the effort usually fails). Do a #undef printf to get back the real printf and not a hook into perl.
        Perl usually hooks libc stuff like printf and malloc on win32 to make very old thread-unsafe XS code, thread-safe (the effort usually fails). Do a #undef printf to get back the real printf and not a hook into perl.

        Wouldn't that affect the msvc build as well as the gcc build?

        Anyhow, I tried it:

        #include <EXTERN.h> #include <perl.h> EXTERN_C void xs_init (pTHX); EXTERN_C void boot_DynaLoader (pTHX_ CV* cv); EXTERN_C void boot_Win32CORE (pTHX_ CV* cv); EXTERN_C void xs_init(pTHX) { char *file = __FILE__; dXSUB_SYS; /* DynaLoader is a special case */ newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); newXS("Win32CORE::bootstrap", boot_Win32CORE, file); } #undef printf static PerlInterpreter *my_perl; /*** The Perl interpreter ***/ int main(int argc, char **argv, char **env) { printf( "So far, so good!\n" ); 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(); return 0; }

        And still the gcc build produces no output:

        C:\test>gcc_bld_hello.c.pl18 Building Hello gcc -Wall -mwindows -o hello hello.c -s -O2 -DWIN32 -DWIN64 -DCONSER +VATIVE -DPERL_TEXTMODE_SCRIPTS -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLIC +IT_SYS -DUSE_PERLIO -fno-strict-aliasing -mms-bitfields -I"C:\Perl5. +18\perl\lib\CORE" -s -L"C:\Perl5.18\perl\lib\CORE" -L"C:\Perl5 gw32\lib\libmpr.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\libwinmm.a C:\P +erl5.18\c\x86_64-w64-mingw32\lib\libversion.a C:\Perl5.18\c\x86_64-w6 +4-mingw32\lib\libodbc32.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\libodb +ccp32.a C:\Perl5.18\c\x86_64-w64-mingw32\lib\libcomctl32.a In file included from C:\Perl5.18\perl\lib\CORE/sys/socket.h:180:0, from C:\Perl5.18\perl\lib\CORE/win32.h:386, from C:\Perl5.18\perl\lib\CORE/win32thread.h:4, from C:\Perl5.18\perl\lib\CORE/perl.h:2869, from hello.c:2: C:\Perl5.18\perl\lib\CORE/win32.h:391:26: warning: "/*" within comment + [-Wcomment] C:\Perl5.18\perl\lib\CORE/win32.h:392:33: warning: "/*" within comment + [-Wcomment] In file included from C:\Perl5.18\perl\lib\CORE/win32thread.h:4:0, from C:\Perl5.18\perl\lib\CORE/perl.h:2869, from hello.c:2: C:\Perl5.18\perl\lib\CORE/win32.h:391:26: warning: "/*" within comment + [-Wcomment] C:\Perl5.18\perl\lib\CORE/win32.h:392:33: warning: "/*" within comment + [-Wcomment] C:\test>.\hello.exe hello.pl18 C:\test>.\hello.exe hello.pl18 C:\test>

        Whereas the cl build does what is expected:

        C:\test>cl /W3 hello.c -DWIN32 -I"C:\Perl64\lib\CORE" C:\Perl64\lib\C +ORE\perl510.lib Microsoft (R) C/C++ Optimizing Compiler Version 15.00.21022.08 for x64 Copyright (C) Microsoft Corporation. All rights reserved. hello.c Microsoft (R) Incremental Linker Version 9.00.21022.08 Copyright (C) Microsoft Corporation. All rights reserved. /out:hello.exe hello.obj C:\Perl64\lib\CORE\perl510.lib C:\test>.\hello.exe hello.pl So far, so good! Hello from MSWin32 5.010001

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Attempt to embed Perl in C on Windows
by LanX (Saint) on Dec 21, 2014 at 14:15 UTC
    > this is a cross-posting from StackOverflow

    Cross-postings are fine as long as you mark all with cross-links.

    So please also update your original post in SO with a link to this new thread here.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

      Done (in a comment).