1. Perl
  2. XS
  3. here

How to use C ++ library from XS

Perl's XS also allows you to call libraries written in C ++ . Just specify "g ++" for C ++ for the compiler and linker.

Create a module with h2xs

First, create a module for XS with h2xs.

h2xs -A -n SomeModule

This will create a directory called "SomeModule". The following files and directories will be created.

Changes
lib/Makefile.PL
MANIFEST
ppport.h
README
SomeModule.xs
t/</pre>

<h3>Creating a C ++ library</h3>

Let's create a C ++ library. Create a header file and a source file.

<b> mylib.h </b>

This is a header file. Declaring mylib_print.

<pre>
void mylib_print();

mylib.cpp

C ++ source file. mylib_print is a function that outputs the string "mylib_cpp". Note that the file name extension is ".cpp".

# include <iostream>

void mylib_print() {
  std::cout << "mylib_cpp\n";
}

Save the header and source files and place them in the same directory where the XS files are located.

XS file description

Let's write an XS file. I am calling mylib_print.

# include "EXTERN.h"
# include "perl.h"
# include "XSUB.h"

# include "ppport.h"
# include "mylib.h"

MODULE = SomeModule PACKAGE = SomeModule

void
foo (...)
  PPCODE:
{
  mylib_print();
  XSRETURN (0);
}

Modify Makefile.PL

Next, let's modify Makefile.PL a little. The bottom "OBJECT" option is commented out by default, so uncomment it. If you set "$(O_FILES)", all C language source files and C ++ source files in the current directory will be compiled.

Then change the compiler and linker to "g ++". The compiler can be set with the "CC" option, and the linker can be set with the "LD".

use ExtUtils::MakeMaker;
use strict;
use warnings;

WriteMakefile (
    NAME =>'SomeModule',
    # VERSION_FROM =>'lib/SomeModule.pm', finds $VERSION
    # PREREQ_PM => {}, e.g., Module::Name => 1.1
    # ($] >= 5.005? # Add these new keywords supported since 5.005
      # (ABSTRACT_FROM =>'lib/SomeModule.pm', retrieve abstract from module
       AUTHOR =>'A. U. Thor <kimoto@sakura.ne.jp>') :()),
    # LIBS => [''], e.g., '-lm'
    # DEFINE =>'', e.g., '-DHAVE_SOMETHING'
    # INC =>'-I.', e.g., '-I. -I/usr/include/other'
    # OBJECT =>'$(O_FILES)', link all the C files too
    CC =>'g ++',
    LD =>'g ++',
);

Test script

Create a test script. This should be in the same directory where the XS files are located.

use strict;
use warnings;

use SomeModule;

SomeModule::foo ();

Compile and run

Let's compile and run it.

perl Makefile.PL
make
perl -Mblib test.pl

If the output is as follows, it is successful.

mylib_cpp

Now you can use the C ++ library from the XS file.

FAQ

Is it possible to write C ++ directly in the XS file?

I can do it. However, it tends to cause problems such as collision with Perl symbols, so I think it is better to describe it in the source file and header file.

Is it possible to use a mixture of C language libraries and C ++ libraries?

C ++ is almost upward compatible with C language, so I think it's okay if the C language library can be compiled with g ++. If not, I'm not sure.

Related Informatrion