Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^4: Extending perl with C dynamic library.

by Martin90 (Sexton)
on Aug 20, 2013 at 17:16 UTC ( #1050211=note: print w/ replies, xml ) Need Help??


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

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


Comment on Re^4: Extending perl with C dynamic library.
Select or Download Code
Re^5: Extending perl with C dynamic library.
by BrowserUk (Pope) on Aug 20, 2013 at 19:04 UTC
    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.

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

        This is a Perl forum.


        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.
Re^5: Extending perl with C dynamic library.
by syphilis (Canon) on Aug 20, 2013 at 23:51 UTC
    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;
        How to make it possible to use this code from shared library ?

        I think (untested) it's just that the XS file that Inline generates specifies "PACKAGE = main", whereas for your purposes here, it needs to specify "PACKAGE = Example". Specifying NAME => 'Example' sets "MODULE = Example", but doesn't set PACKAGE appropriate to your needs.

        If, having installed InlineX::C2XS you put that C code (only the C code - nothing else) into ./src/Example.c, create a directory named (say) ./MyMod, and run this script:
        use warnings; use strict; use InlineX::C2XS qw(c2xs); c2xs('Example', 'Example', './MyMod');
        Then you should get a correct XS file for inclusion in a perl module/package named 'Example'.
        You can also provide arguments to c2xs() to auto-generate a Makefile.PL, Example.pm, and MANIFEST files, thereby making it easy to build your 'Example' module in the usual manner (and with no dependency upon Inline::C).

        As regards the "main" function in the code you provided, it seems to be working fine - but it's just another subroutine, and it's therefore best to call it something other than "main" (to avoid confusion).

        Cheers,Rob

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-09-20 14:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (159 votes), past polls