Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Extending perl with C dynamic library.

by Martin90 (Sexton)
on Aug 19, 2013 at 09:53 UTC ( #1050003=note: print w/ replies, xml ) Need Help??


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

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`

returns:

"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`

return:

"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 ?


Comment on Re^2: Extending perl with C dynamic library.
Download Code
Replies are listed 'Best First'.
Re^3: Extending perl with C dynamic library.
by BrowserUk (Pope) on Aug 19, 2013 at 12:55 UTC

    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:

      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
        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.
Re^3: Extending perl with C dynamic library.
by syphilis (Canon) on Aug 19, 2013 at 10:43 UTC
    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.)

    Cheers,
    Rob

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2015-08-05 02:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...













    Results (76 votes), past polls