Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Need Help with perlxstut

by ketema (Scribe)
on Sep 27, 2004 at 13:26 UTC ( [id://394157]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks, I need some help with using c functions in s a PERL script. I have read through the perlxstut, and I think I get the concepts, and I was able to get the very first example to work, but not all the others. I have what I think should be a very simple extension. I have two files provided to me by my instructor: hilbert.c and hilbert.h, although they are very long they are reallt just a couple of functions to return a hilbert curve index given a point, and to return coordinates given a hilbert index. I wish to use these functions in a PERL script, but can't seem to get from perlxstut examples to actually applying it to my library. I didn't post the c files here, but will be glad to email them if someone is generous enough to help. Right now I am at the part where I have a Hilbert subdirectory, and I was able to run nmake but I can't use the dll file that was created, (Pretty sure there is nothing in it). If someone could provide or point to a more step by tutorial for xtending PERL with an existing h 7 c file I would ber grateful. If someone is so inclined to provide the tutorial themselves I can email the c header files. Thanks.

Replies are listed 'Best First'.
Re: Need Help with perlxstut
by Corion (Patriarch) on Sep 27, 2004 at 13:29 UTC

    Start out by using your C files via Inline::C and getting everything to work there. Then take the .XS file that Inline::C creates for you and further customize it, after you have had everything working.

    Going the traditional route of manually writing your XS file is most likely best done via the h2xs program which should create an XS file from your .h file. If that works for you, you can bypass the Inline::C step.

      Thanks. I think this module will help greatly, and I think i'm going to pick up Advanced Perl Programming to get better at the "traditional" way. Should have know there was a module for this though...duh Thanks Again.
      I made some code:
      @Coord[3]; hilbert_i2c(3,10,190355,@Coord); print join(" ",@Coord); @Coord[2] = 35.5; @Coord[1] = 40; @Coord[0] = 36.8; print hilbert_c2i(3,10,@Coord), "\n"; use Inline C => Config => LIBS => '-lhilbert'; use Inline C => <<'END_OF_C_CODE'; Lots of C code Here with the above functions implemented END_OF_C_CODE
      The script chugs along, then errors out on the compile with the following:
      E:\Profiles\ADMINI~1.TEM\MYDOCU~1\VISUAL~1\MACHIN~1>perl Hilbertknn.pl Can't locate auto/main/hilbert_i2c.al in @INC (@INC contains: E:\Profi +les\ADMINI ~1.TEM\MYDOCU~1\VISUAL~1\MACHIN~1\_Inline\lib E:/Perl/lib E:/Perl/site +/lib .) at Hilbertknn.pl line 2
      "Can't locate auto/main/hilbert_i2c.al in @INC" being the most relevant part obviously, but I am at a loss as to where the proper files should be. The hilbert.c file is in the inline portion of the perl script, and the Hilbert.h files is in the same directory. Where else should it go?

        When you say it chugs along what you mean is that it fails to compile. For a start your calling syntax is just not going to work. Next your knowledge of C seems imperfect. A library is compiled binary code. To use it you need the headers and the library. You don't have that, you have the C headers and main source code. If you are goind to inline it you need the headers and then the main source inline.

        When you want to link C code into Perl the main issue is basically type conversion, from the perl SV, AV or HV types, into C types and then back again. The main advantage of Inline::C is it will handle some simple type conversions for you automatically, but it only handles really simple ones. With XS you are expected to do it yourself.

        Your error will probably be typemap/conversion related. You will also need to pass an array ref of your N dimensional coordinates and decant the values in your C. See Re: Using c executable in Perl Script for an example of how to do that.

        cheers

        tachyon

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-19 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found