Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re^5: Extending perl with C dynamic library.

by syphilis (Chancellor)
on Aug 20, 2013 at 23:51 UTC ( #1050267=note: print w/replies, xml ) Need Help??

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

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


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

    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 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,, 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).


        Problem solved ;)

        @syphilis you were right Module = main was the problem ! Huge thanks for help ;)

        I would like also thank for help all users involved in this subject in particular @BrowserUk and @syphilis !

        All best guys ;)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1050267]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2017-02-23 10:03 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (343 votes). Check out past polls.