Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How to use C Libraries in Perl Script?

by Raj_Padma (Initiate)
on Feb 23, 2012 at 07:51 UTC ( [id://955672]=perlquestion: print w/replies, xml ) Need Help??

Raj_Padma has asked for the wisdom of the Perl Monks concerning the following question:

Hi , I am using XS method to call the C functions in Perl. but I am unable to call the C functions into C. Please let me know if there is any method such I can use C functions in perl script. Thanks, Raj

Replies are listed 'Best First'.
Re: How to use C Libraries in Perl Script?
by davido (Cardinal) on Feb 23, 2012 at 08:40 UTC

    Without any code I wouldn't want to try to guess from all the many ways that XS code could fail to "do what you want." I can say, however, that if all you need is Perlish access to a C library, the work is 90% done when you use Inline::C correctly.

    In specific, look at the ...

    Config => ENABLE => 'AUTOWRAP' => LIBS => '-lwhatever';

    ...configuration options.

    Here's a snippet from the Inline::C-Cookbook:

    package MyTerm; use Inline C => Config => ENABLE => AUTOWRAP => LIBS => "-lreadline -lncurses -lterminfo -ltermcap "; use Inline C => q{ char * readline(char *); }; package main; my $x = MyTerm::readline("xyz: ");

    It's never quite as easy as it looks. You will need to be aware of how the library allocates memory on the heap so that you can free it appropriately (to avoid leaks). And that's just the start of the "gotchas". But Inline::C does handle a lot of the boilerplate for you.


    Dave

Re: How to use C Libraries in Perl Script?
by Anonymous Monk on Feb 23, 2012 at 08:03 UTC
Re: How to use C Libraries in Perl Script?
by JavaFan (Canon) on Feb 23, 2012 at 10:14 UTC
    but I am unable to call the C functions into C
    Either you mean something else than what you write, or you have a C problem, not a Perl problem.
Re: How to use C Libraries in Perl Script?
by sundialsvc4 (Abbot) on Feb 24, 2012 at 14:43 UTC

    Follow all of those links above and read them carefully.   Start by building a library of the functions; a library that you want to call from either environment.   Then, make sure that you know how to successfully call them from C ... that is to say, Perl is not involved yet.   Next, use the procedures described above to create the necessary “wrapper” that is needed to allow the Perl environment to make the call.   Notice that this is another layer of software, mostly “automagically built.”   But you really need to be sure that you have the direct-from-“C” scenario fully debugged first, because problems in that simpler scenario will of course also directly impact the other scenario. If you let them be jumbled-up, in what is already unfamiliar to-you territory anyhow, it’s unnecessarily difficult to sort them out.

    Another trick that I like to use is to pick a package from CPAN that I already know uses an external library.   Then, I go into CPAN and look at it.   Then, of course taking great care not to touch anything, I will look around at actual, known-to-be working examples (and corresponding test cases), all of which I know will be “right there.”   Sometimes, an example’s worth a thousand words of documentation.

Log In?
Username:
Password:

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

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

    No recent polls found