如何在Windows上正确地将Perl解释器嵌入C++应用程序中

5
我有一个大约15,000行的Perl脚本,想要通过使用PerlInterpreter从用C++编译的Windows可执行文件中执行它。
我尝试按照这些说明进行操作。
我下载了Perl 5.18源码并包含了核心(安装)目录,用于perl.hEXTERN.h,以及core/win32core/win32/include
然后我尝试在Visual Studio 2013中编译简单的C++项目。
#include <EXTERN.h>               /* from the Perl distribution     */
#include <perl.h>                 /* from the Perl distribution     */

static PerlInterpreter *my_perl;  /***    The Perl interpreter    ***/

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

该项目无法编译,出现了数百个错误,但最常见的错误是:
missing config.h file included by perl.h

所以我在perl.h中添加了#define PERL_MICRO声明

这将错误数量减少到20个

然而,所有的错误都来自于Windows SDK文件string.hguidedef.h,因此我不知道这些错误的起源

有人能否提供我有关如何创建和构建用于Perl脚本的C++解释器的任何信息吗?


剩下的错误为

Error   10  error C3861: 'my_memcmp': identifier not found  c:\program files (x86)\windows kits\8.1\include\shared\guiddef.h    161 1   

Error   3   error C2733: 'memmove_s' : second C linkage of overloaded function not allowed  c:\program files (x86)\microsoft visual studio 12.0\vc\include\string.h 136 1   

Error   4   error C2733: 'memmove' : second C linkage of overloaded function not allowed    c:\program files (x86)\microsoft visual studio 12.0\vc\include\string.h 139 1   

Error   2   error C2733: 'memcpy_s' : second C linkage of overloaded function not allowed   c:\program files (x86)\microsoft visual studio 12.0\vc\include\string.h 57  1   

Error   1   error C2733: 'memcpy' : second C linkage of overloaded function not allowed c:\program files (x86)\microsoft visual studio 12.0\vc\include\string.h 55  1   

Error   8   error C2732: linkage specification contradicts earlier specification for 'strstr'   c:\program files (x86)\microsoft visual studio 12.0\vc\include\string.h 228 1   

Error   7   error C2732: linkage specification contradicts earlier specification for 'strrchr'  c:\program files (x86)\microsoft visual studio 12.0\vc\include\string.h 226 1   

Error   6   error C2732: linkage specification contradicts earlier specification for 'strpbrk'  c:\program files (x86)\microsoft visual studio 12.0\vc\include\string.h 224 1   

Error   5   error C2732: linkage specification contradicts earlier specification for 'strchr'   c:\program files (x86)\microsoft visual studio 12.0\vc\include\string.h 222 1   

Error   9   error C2732: linkage specification contradicts earlier specification for 'memchr'   c:\program files (x86)\microsoft visual studio 12.0\vc\include\string.h 233 1   

3
您似乎正在随意更改内容,以试图摆脱错误消息;这不是一个好的工作方式。这些消息本身并不是问题,但它们提醒您需要解决的问题。我不确定如何解决您的问题,但篡改Perl发行文件并不是其中之一。如果您想在构建中定义PERL_MICRO,则需要修改您的Visual Studio项目来定义它。头文件形成了Perl编译器/解释器接口的定义,您不能随意更改它们。 - Borodin
1个回答

5
我将描述我所知道的一些关于这个主题的事情。我希望这会有所帮助。
首先,您应该指定您在Windows上使用哪个Perl。至少我知道有两种: 其次,您要尝试实现的是“在C/C++应用程序中嵌入Perl解释器”。我提到这一点,以便您在搜索相关文档时可以参考它。 perlembed文档提供了关于此的一般信息。
如果您想在ActivePerl上的Windows上执行此操作,则可以查看Embedding Perl Under WIN32

对于Strawberry Perl,Perlmonks上的某个人在这里提供了一个在Windows上嵌入Perl的工作示例。

同时,请注意可能需要按照perlwin32 perl文档页面设置MSVC编译器的适当路径。

最后,还有一本关于此主题的书,名为Extending and Embedding Perl,您可能想查看一些有关此主题的详细信息。在这本书中,第8章总体上讨论了在C中嵌入Perl,而第9章则涉及一个嵌入案例研究。

更新

阅读ExtUtils::MakeMaker文档时,我发现了这部分内容,它看起来很有趣:

如果您正在将Perl作为库嵌入到其他应用程序中运行实验,您可能会发现MakeMaker不足以满足需求。您最好看一下ExtUtils :: Embed,它是一组用于嵌入的实用程序集。

特别地,ExtUtils :: Embed涉及嵌入Perl到C / C ++应用程序中的实用程序。

我自己还没有尝试过,但我认为它可能会有所帮助。一旦我有时间研究这个问题,我就会回来提供更多细节。

更新:

以下是一些具有嵌入式Perl解释器的应用程序列表:


谢谢,我会在这些文档中寻找解决方案。我已经安装了Strawberry Perl和Activestate Perl。至于源代码,我不确定我下载的是哪个版本,因为网站http://www.perl.org/get.html(源代码部分)没有指定版本。目前,我已决定放弃Perl并用C重新编写应用程序,因为那是我的应用程序最终目标。 - Mich

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接