How can I call functions in another C source file from a Perl XS module?

I am building an XS extension with Perl. I have two files:

  • C header file ( .h )
  • C source file ( .c )

Currently, I have put all the code in the C file before Model= in the XS file and wrapped the functions that I want after Model= .

Complexity works without problems, and I can call specific functions from perl.

But I want to separate the .xs file from C.

I want the .xs file .xs contain only transfer functions, and these functions will call the functions in the .c file, but when I do this and run the dmake command, I get an error code 129 undefined link to the parse.c file.

I tried to include the .c file using the C and OBJECT properties for WriteMakerFile, and still get an error message to split the xs file into 2 c files and other xs that complete the c function that are part of .c with ExtUtils :: MakeMaker .

Examples will be appreciated.

+7
perl perl-module perl-xs
source share
1 answer

It is pretty simple:

hello.h

 #ifndef H_HELLO const char *hello(void); #define H_HELLO #endif 

hello.c

 const char * hello(void) { return "Hello"; } 

Example.xs

 #define PERL_NO_GET_CONTEXT #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" #include "hello.h" MODULE = My::Example PACKAGE = My::Example PREFIX = MY_ PROTOTYPES: DISABLE const char * MY_hello() CODE: RETVAL = hello(); OUTPUT: RETVAL 

t / My -Example.t

 use strict; use warnings; use Test::More; BEGIN { use_ok('My::Example') }; is(My::Example::hello(), 'Hello', 'hello returns "Hello"'); done_testing; 
  [~ / tmp / My-Example] $ prove -vb t / My-Example.t
 t / My-Example.t ..
 ok 1 - use My :: Example;
 ok 2 - hello returns "Hello"
 1..2
 ok
 All tests successful.
 Files = 1, Tests = 2, 0 wallclock secs 
 (0.04 usr 0.01 sys + 0.03 cusr 0.00 csys = 0.08 CPU)
 Result: PASS 

Makefile.PL was created by h2xs . The only thing I changed is to uncomment the following line:

  # Un-comment this if you add C files to link with later:
  OBJECT => '$ (O_FILES)', # link all the C files too 
+9
source share

All Articles