in reply to Re: embedded perl calls c subroutines
in thread embedded perl calls c subroutines

yeah plaese tell me about the less "strict" way. i want to play around anyway ... but i'm going in circles, i tried h2xs and swig already, but get a) problems with dynaloader (I DON'T want dynamic loading anyway ;-) and b) segmentation faults ...

BTW: Inline::C is not what i'm looking for. the c-subroutine is already compiled with the c program that starts the perl interpreter. i used Inline::C in other projects it's quite nice and it does caching of compiled code.

  • Comment on Re: Re: embedded perl calls c subroutines

Replies are listed 'Best First'.
Re: Re: Re: embedded perl calls c subroutines
by Courage (Parson) on Aug 13, 2002 at 15:27 UTC
    Do not afraid to shoot your leg? You're brave man!

    I did "glueing" that way: (nothing unusual, I just read perlembed and related documentation)

    1. #include <EXTERN.h>, #include <perl.h> somewhere at the very top of C program. #include "xsub.h" somewhere near the end, and after that all "glue" functions are placed. All those header files are in perl/lib/CORE/* directory.
    2. Write "glue" functions like following example:
      XS(TStringsTIEARRAY) { dXSARGS; SV *ssv=ST(0); // "G::TStrings" void* tmp[4]; memset(tmp,0,sizeof(tmp)); tmp[2] = 0; ((TStrings*)(tmp[0])) = TyingStrings; ((TComponent*)(tmp[1])) = TyingComp; ((int)(tmp[2])) = TyingOpt; SV *tsv; tsv = newSVpv((char*)&tmp[0],16); STRLEN len; char *ptr = SvPV(ssv,len); HV *stash = gv_stashpvn(ptr,len, TRUE); SV *rsv = newRV_inc(tsv); sv_bless(rsv,stash); ST(0) = rsv; XSRETURN(1); }
      Note usage of XS macro, "real" name of function will be more complicated after expansion of that macro.
    3. initialize your perl instance(s) as perlembed.pod says you to.
    4. After initializing it/them, in order to make those functions visible to perl, do something like:
      newXS("G::TStrings::TIEARRAY", TStringsTIEARRAY, "BCB+Perl interna +l");
    5. you may want to also run following:
      newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, "blablabla") +;
      to enable dynamic loading of modules.
    And remember, I do not enCourage you to do that :)

    Courage, the Cowardly Dog
    things, I do for love to perl...

      thanks for the hardcore version, nice to see the simple way, but i'll do the middle way between h2xs and this one, which would be the one you mentioned before. i'll write the xs glue at the bottom of main.xs convert it with xsubpp and link with the help of ExtUtils. so i stay portable (between perl versions and/or platforms) ;-) thanks for your help, i'll vote for your solution as soon as i've set up a real account :-)
        You're right: choosing may be not easiest, but robust and supported way, you'll get most portable and clean final result.

        Best wishes,
        Courage, the Cowardly Dog