Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Extending perl with C dynamic library.

by spx2 (Deacon)
on Aug 19, 2013 at 07:39 UTC ( #1049983=note: print w/replies, xml ) Need Help??

in reply to Extending perl with C dynamic library.

If you want to learn XS, there's a good book on it, I mean this one : Extending and Embedding Perl

Also, use proper tooling such as h2xs to generate the skeleton you need, and you can build on that.

  • Comment on Re: Extending perl with C dynamic library.

Replies are listed 'Best First'.
Re^2: Extending perl with C dynamic library.
by Martin90 (Sexton) on Aug 19, 2013 at 09:53 UTC

    syphilis, thanks that helped and .so are smaller ;)

    spx2 thanks for good resource !

    Unfortunately I encountered another problem, this time when I try to add perl compiler to C.

    #!/usr/bin/perl use strict; use warnings; use Config; use Inline C => Config => BUILD_NOISY => 1, CCFLAGS => $Config{ccflags +}." -DDEBUG=1"; use Inline C => <<'END_C', NAME => 'Example', CLEAN_AFTER_BUILD =>0; static PerlInterpreter *my_perl; int main(int argc, char **argv) { char* command_line[] = {"", "-e", "print \"Hello from C!\\n\";"}; my_perl = perl_alloc(); perl_construct(my_perl); perl_parse(my_perl, NULL, 3, command_line, (char **)NULL); perl_run(my_perl); perl_destruct(my_perl); perl_free(my_perl); return 0; } int add( int a, int b ) { int result; result = a + b; return result; } int hello() { printf("Hello World\n"); } END_C sub message { print "Everything went well" } &message;

    On windows this code fail:

    "Dmake error code 129, while making \/patch_here/Example.dll" "A problem was encountered while attepting to compile and install your inline c code. The command that failed was dmake."

    On Linux (CentOS 6) program compile successfully but problem appears in next step:

    cc -o Example.o -c Example.c `perl -MExtUtils::Embed -e ccopts`


    "Example.xs:4:20: error: INLINE.h: No such file or directory Example.c: In function XS_main_main: Example.c:55: warning: initialization makes pointer from integer without a cast"

    Because Inline produce their own .o file I try to compile their .o file, again with no success:

    cc -o Example Example.o `perl -MExtUtils::Embed -e ldopts`


    "Example.o: In function `XS_main_main': /home/admin/Embed/_Inline/build/Example/Example.c:55: undefined reference to `XS_unpack_charPtrPtr' collect2: ld returned 1 exit status"

    I have no idea what is wrong here, looking for your help again monks ;)

    Also, it is possible to implement full perl subroutine code to C code or just add pointer that refer to perl subroutine somewhere else ?

      You are trying to create a dll with a main entrypoint. No wonder it fails.

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Ok after hours of testing I resolved most of the problems. My main focus was to implment some necessary code into C but I can't do this even with the simplest code from documentation.

        Here is an example:
        #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include <stdio.h> #include "ppport.h" static PerlInterpreter *my_perl; int main(int argc, char **argv, char **env) { SV *cvrv = eval_pv("sub { print 'You will not find me cluttering any n +amespace!' }", TRUE); char *args[] = { NULL }; PERL_SYS_INIT3(&argc,&argv,&env); my_perl = perl_alloc(); perl_construct(my_perl); perl_parse(my_perl, NULL, argc, argv, NULL); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; /*** skipping perl_run() ***/ call_sv(cvrv, G_VOID|G_NOARGS); perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); } MODULE = Example PACKAGE = Example
        NOTE: I back to XS and "make" since Inline has problems with compile this code. With make I compile this code fine to Example.c then with

        cc -o interp Example.c `perl -MExtUtils::Embed -e ccopts -e ldopts`

        produce executable file "interp". Up to this point everything went ok. The problem occur when i try tu run "interp":

        % ./interp

        returns :

        Segmentation fault.

        I think problem is in line: SV *cvrv = eval_pv("sub { print 'You will not find me cluttering any namespace!' }", TRUE);"

        Documentation corresponding with this code:

        Do you know what is wrong ?

        Thanks ;)
      On windows this code fail

      On Windows, if (in your code) I replace int main(int argc, char **argv) { with int main(void) { then everything goes fine.
      At least, I get output of Everything went well.

      Does that help ? (I'm a bit lost as regards where this is heading.)


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1049983]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2022-12-01 06:42 GMT
Find Nodes?
    Voting Booth?