嵌入式C程序中在Perl中调用C函数 [英] Calling C function from Perl within embedded C application
问题描述
好吧,这是一个非常有趣的问题,并有可能没有任何简单的方法来做到这一点,但想我会决定,修改Perl是我基本的答案之前,有扔了这一点。
Ok, this is a very interesting question and there may not be any easy way to do this but figured I would throw this out there before deciding that modifying Perl is my underlying answer.
所以,我已经得到了在嵌入式的方式调用Perl脚本C应用程序。这一切工作正常,很正常,它是pretty真棒,我可以在信息传递和获取信息的退了出去。然而,到现在我的下一个征服的;我需要让我的嵌入的脚本(县)能够调用C的应用程序,最初被称为IT中的某些功能。
So I've got a C application that calls Perl scripts in an embedded fashion. This all works fine and dandy and it's pretty awesome that I can pass information in and get information back out. HOWEVER, now onto my next conquest; I need to allow my embedded script(s) to be able to call some functions within the C application that ORIGINALLY CALLED IT.
这是重要的,因为XSUB将要求它是外部库;但我不希望它是一个外部库我希望它是C函数(S)直接调用。现在,也许这可以通过XSUB完成,并且我刚刚阅读和理解是错误的。
This is important because XSUB would require it to be an external library; but I don't want it to be an external library I want it to be a direct call to the C function(s). Now maybe this can be done via XSUB and I've just been reading and understanding it wrong.
Application -(run)-> Perl
Application <-(function_x())- Perl
Application -(returnfunction_x)-> Perl
这不能是外部库的原因是因为我依靠存储在应用程序内的完全是创建的数据/
The reason this cannot be an external library is because I am relying on data that is solely created/stored within the application.
推荐答案
XSUBs其实不需要有被外部库。他们仅仅提供调用从Perl的空间c函数,并映射C和Perl的之间的调用约定提供一些便利的功能。
XSUBs actually don't require there to be an external library. They merely provide the ability to call to a c function from perl space, and provide some convenience in mapping the calling conventions between C and Perl.
所有你需要做的是注册XSUBs你PTER要嵌入编译成perl的跨$ P $嵌入应用程序。
All you need to do is register XSUBs you compiled into the embedding application with the perl interpreter you're embedding.
#include "XSUB.h"
XS(XS_some_func);
XS(XS_some_func)
{
dXSARGS;
char *str_from_perl, *str_from_c;
/* get SV*s from the stack usign ST(x) and friends, do stuff to them */
str_from_perl = SvPV_nolen(ST(0));
/* do your c thing calling back to your application, or whatever */
str_from_c = some_c_func(str_from_perl);
/* pack up the c retval into an sv again and return it on the stack */
mXPUSHp(c_str);
XSRETURN(1);
}
/* register the above XSUB with the perl interpreter after creating it */
newXS("Some::Perl::function", XS_some_func, __FILE__);
当嵌入Perl,这样的事情通常是在传递给 parse_perl
的xs_init函数来完成。
When embedding perl, this sort of thing is usually done in the xs_init function you pass to parse_perl
.
EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
static void
xs_init (pTHX)
{
newXS("Some::Perl::function", XS_some_func, __FILE__);
/* possibly also boot DynaLoader and friends. perlembed has more
* details on this, and ExtUtils::Embed helps as well. */
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
}
perl_parse(my_perl, xs_init, argc, my_argv, NULL);
之后,你就可以向XSUB称呼其为部分:: Perl的::功能
从perl的空间,反过来说XSUB可以自由地回电以任何方式应用程序就是了。
After that you'll be able to call to the XSUB as Some::Perl::function
from perl space, and that XSUB in turn is free to call back to your application in any way it wants to.
这篇关于嵌入式C程序中在Perl中调用C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!