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

loading C shared object with perl

by kopolov (Acolyte)
on Mar 23, 2016 at 14:12 UTC ( [id://1158627]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am trying to load a simple shared object into perl, based on this link:

http://www.lemoda.net/xs/xs-intro/geometry.html

This is my header file (hypotenuse.h):

double hypotenuse (double val_x, double val_y);
This is my source file (hypotenuse.c):
#include "hypotenuse.h" #include <math.h> double hypotenuse (double val_x, double val_y) { return sqrt (val_x*val_x + val_y*val_Y); }
this is my XS (Geometry.xs):
#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" #include "hypotenuse.h" MODULE = Geometry PACKAGE = Geometry PROTOTYPES: ENABLE double hypotenuse(val_x, val_y) double val_x double val_y
This is my Geometry.t file
# Before `make install' is performed this script should be runnable wi +th # `make test'. After `make install' it should work as `perl Geometry.t +' ######################### # change 'tests => 1' to 'tests => last_test_to_print'; use Test::More tests => 1; BEGIN { use_ok('Geometry') }; #is (Geometry::hypotenuse (3, 4), 5); ######################### # Insert your test code below, the Test::More module is use()ed here s +o read # its man page ( perldoc Test::More ) for help writing this test scrip +t.
The steps I took:

1. Run h2xs -A -n Geometry (created Geometry folder, and needed base files)

2. place all files above in this folder (the Geometry.t is auto generated, and so is Geometry.xs, which I edit)

3. Run "perl Makefile.PL". completed successfully

Here is the output:

Checking if your kit is complete... Looks good Writing Makefile for Geometry
4. Run "make". completed successfully

Here is the output:

cp lib/Geometry.pm blib/lib/Geometry.pm gcc -c -I. -D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -fst +ack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET +_BITS=64 -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fst +ack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -DVERSI +ON=\"0.01\" -DXS_VERSION=\"0.01\" -fPIC "-I/usr/lib64/perl5/CORE" G +eometry.c Running Mkbootstrap for Geometry () chmod 644 Geometry.bs rm -f blib/arch/auto/Geometry/Geometry.so gcc -shared -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions - +fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic Geomet +ry.o -o blib/arch/auto/Geometry/Geometry.so \ \ chmod 755 blib/arch/auto/Geometry/Geometry.so cp Geometry.bs blib/arch/auto/Geometry/Geometry.bs chmod 644 blib/arch/auto/Geometry/Geometry.bs Manifying blib/man3/Geometry.3pm
5. Run "make test" (to execute file Geometry/t/Geometry.t)

NOW IT FAILS ...

PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_h +arness(0, 'blib/lib', 'blib/arch')" t/*.t t/Geometry.t .. 1/1 # Failed test 'use Geometry;' # at t/Geometry.t line 9. # Tried to use 'Geometry'. # Error: Can't load '/gpfs/haifa-p4/08/tm/team/hagaih/geometry/Ge +ometry/blib/arch/auto/Geometry/Geometry.so' for module Geometry: /gpf +s/haifa-p4/08/tm/team/hagaih/geometry/Geometry/blib/arch/auto/Geometr +y/Geometry.so: undefined symbol: hypotenuse at /usr/lib64/perl5/DynaL +oader.pm line 200. # at (eval 4) line 2 # Compilation failed in require at (eval 4) line 2. # BEGIN failed--compilation aborted at (eval 4) line 2. # Looks like you failed 1 test of 1. t/Geometry.t .. Dubious, test returned 1 (wstat 256, 0x100) Failed 1/1 subtests Test Summary Report ------------------- t/Geometry.t (Wstat: 256 Tests: 1 Failed: 1) Failed test: 1 Non-zero exit status: 1 Files=1, Tests=1, 0 wallclock secs ( 0.01 usr 0.02 sys + 0.01 cusr + 0.00 csys = 0.04 CPU) Result: FAIL Failed 1/1 test programs. 1/1 subtests failed. make: *** [test_dynamic] Error 1
I made sure the file exists:
hagaih@lnx-tm2: geometry/Geometry/t 84 > ls /gpfs/haifa-p4/08/tm/team +/hagaih/geometry/Geometry/blib/arch/auto/Geometry/Geometry.so /gpfs/haifa-p4/08/tm/team/hagaih/geometry/Geometry/blib/arch/auto/Geom +etry/Geometry.so*
What am I doing wrong ?

Replies are listed 'Best First'.
Re: loading C shared object with perl
by syphilis (Archbishop) on Mar 24, 2016 at 00:53 UTC
    What am I doing wrong ?

    Did h2xs generate that XS file ? Your XS file is missing the following code:
    double hypotenuse (double val_x, double val_y) { return sqrt (val_x*val_x + val_y*val_y); }
    And that code needs to be placed between the line:
    #include "hypotenuse.h"
    and the line:
    MODULE = Geometry PACKAGE = Geometry
    You may subsequently find that you also need to link to libm (-lm) in order to resolve "sqrt".

    Cheers,
    Rob

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-24 19:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found