尝试嵌入的Perl用C在Windows

问题描述:

我试图从 Deploy a Perl Application on Windows尝试嵌入的Perl用C在Windows

用一个简单的“Hello.pl”(只是打印“Hello”到标准输出)运行下面的示例。

它失败。 .exe文件已创建,但不会生成任何输出。

也许这是我的基本误解。你能否指点我正确的方向?顺便说一句。项目文件夹中的“包含所有依赖关系的lib文件夹”是空的,因为“hello.pl”中没有模块。这是一个正确的假设吗?

非常感谢!

的hello.c的文件:

#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(); 
} 

Perl的文件生成编译器命令:

#!/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 -mwindows -o K:\Scripts\Embed\Hello_3\hello K:\Scripts\Embed\Hello_3\hello.c', 
&ccopts, &ldopts); 

print STDOUT $gcc_cmd , "\n"; 
system($gcc_cmd); 

输出:

---------------------------------------------- 
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\Hello_3\hello K:\Scripts\Embed\Hello_3\hello.c -s -O2 -DWIN32 -DPERL_TEXTMODE_SCRIPTS -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -fno-strict-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_portable\c\i686-w64-mingw32\lib\libmoldname.a C:Perl_516_portable\c\i686-w64-mingw32\lib\libkernel32.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\libuser32.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\libgdi32.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\libwinspool.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\libcomdlg32.a C:\Perl_516_portable\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\liboleaut32.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\libnetapi32.a C:\Perl_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-mingw32\lib\libmpr.a C:\Perl_516_portable\c\i686-w64-mingw32\lib\libwinmm.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: "/*" within comment [-Wcomment] 
C:\Perl_516_portable\perl\lib\CORE/win32.h:362:33: warning: "/*" within 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: "/*" within comment [-Wcomment] 
C:\Perl_516_portable\perl\lib\CORE/win32.h:362:33: warning: "/*" within 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] 
+1

今天我发布在PerlMonks上,因为我没有回答:(http://www.perlmonks.org/?node_id=1110956) – 2014-12-21 14:50:46

它不会,如果你是工作在不同于你的脚本和c文件的路径中。删除绝对路径K:\Scripts\Embed\Hello_3\

项目文件夹中的“lib文件夹包含所有依赖项”是空的,因为“hello.pl”中没有模块。这是一个正确的假设吗?

如果hello.pl没有使用任何库,是的。

int main(int argc, char **argv, char **env) 
{ 
    argv[1] = "-Ilib"; 
    argv[2] = "hello.pl"; 
    ... 

这只有在argc是2的情况下才有效,即您向hello.exe提供了2个参数。 你最好检查argc和是否< 2延伸的argv和argc个设置为2,如果< 2.

步入可执行文件gdb,看看有什么地方出了错。然后编译-g

从长远来看,建立的解决方案是使用PAR :: Dist或其中一个商业包装商。在Windows上使用真正的编译器perlcc有点棘手。

+0

非常感谢! – 2015-08-20 15:44:17