Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: Extending perl with C dynamic library.

by BrowserUk (Patriarch)
on Aug 19, 2013 at 12:55 UTC ( [id://1050028]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Extending perl with C dynamic library.
in thread Extending perl with C dynamic library.

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.

Replies are listed 'Best First'.
Re^4: Extending perl with C dynamic library.
by Martin90 (Sexton) on Aug 20, 2013 at 17:16 UTC

    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:

    http://perldoc.perl.org/perlcall.html#Creating-and-Calling-an-Anonymous-Subroutine-in-C

    Do you know what is wrong ?

    Thanks ;)
      Do you know what is wrong ?

      You appear to be trying to embed perl into XS. I don't know of anyone ever having tried that, and I don't know why anyone *would* try that.
      AFAIK, you only ever embed perl into a *C* program - and you do that only when you want to be able to run perl code in that C program.
      If you want to run perl code from XS, you just do a callback to that perl code. Here is a simple Inline::C example based on an example in the perldoc perlcall documentation:
      use strict; use warnings; use Inline C => Config => USING => 'ParseRegExp', BUILD_NOISY => 1; use Inline C => <<'EOC'; void c_foo(void) { dSP; PUSHMARK(SP); call_pv("perl_foo", G_DISCARD|G_NOARGS); } EOC c_foo(); sub perl_foo { print "PID is $$\n"; }
      I see you've found that perlcall documentation. Note that it contains no attempts to embed a perl interpreter into the code.

      (In case you're unaware of it, Inline::C and XS are essentially the same - it's just that Inline::C writes your XS file for you, then compiles the XS code and runs the script.)

      Cheers,
      Rob

        I tried another way which simplify the fact of implement some code to C and then use this code in perl by:

        My Inline code (compiles and works well):

        #! perl -slw use strict; 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; #include <stdio.h> #include <stdlib.h> int add( int a, int b ) { int result; result = a + b; return result; } int main () { /* char *a = "print \"Hello from C!\\n\";"; */ code(); return 0; } END_C print "Everything went well ;)","\n";

        My Example.pm file to load dll / so libraries (works well):

        package Example; use 5.006; use strict; use base qw/Exporter DynaLoader/; our $VERSION = '0.00'; our @EXPORT_OK = qw/hello/; bootstrap Example $VERSION; 1;
      Do you know what is wrong ?

      Yes. Basically, you have no idea what you are doing.

      You are still trying to compile a main() function into a dll.

      Dlls contain and export entrypoints that *must* be dynamically linked to and called from an executable (with a main()).

      You can't use Inline C to build main programs.


      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.

        "You are still trying to compile a main() function into a dll."

        No.I leave windows and compile code on Linux to .C and then to executable.

        "Yes. Basically, you have no idea what you are doing."

        Well, my problem is weak understand of C programming. That's why I am looking for help on this matter ;)

        Now the only question is why it returns Segmentation fault.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-03-29 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found