尝试在Windows上的C中嵌入Perl

时间:2014-11-21 10:08:15

标签: perl

我正在尝试运行以下示例 Deploy a Perl Application on Windows

使用简单的“Hello.pl”(只是将“Hello”打印到STDOUT)。

失败了。 .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 个答案:

答案 0 :(得分:1)

如果您的路径与脚本和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个a​​rgs。 如果&lt;你需要检查argc并扩展argv。如果&lt; 2,则将argc设置为2。 2。

使用gdb进入可执行文件,看看出了什么问题。然后用-g编译。

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