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

Re^6: Extending perl with C dynamic library.

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


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

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;

Replies are listed 'Best First'.
Re^7: Extending perl with C dynamic library.
by syphilis (Chancellor) on Aug 21, 2013 at 22:30 UTC
    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

      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2018-07-23 04:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (459 votes). Check out past polls.

    Notices?