I'm trying to learn SWIG and I'm having some issues getting SWIG to work with perl on a Linux machine. I have the files Dog.h, Crow.h, Animal.i, and libmylib.so. All these files are in the same directory. libmylib.so was compiled using Dog.cpp and Crow.cpp, which reference Dog.h and Crow.h respectively. My Animal.i file is as follows:
%module Animal
%{
/* Includes the header in the wrapper code */
#include "Dog.h"
#include "Crow.h"
%}
/*Parse the header file to generate wrappers */
%include "Dog.h"
%include "Crow.h"
Here are the commands that I'm executing in order to build the perl module:
swig -perl -c++ Animal.i
g++ -shared -fPIC Animal_wrap.cxx -L. -lmylib -I/usr/lib64/perl5/CORE -o _Animal.so
LD_LIBRARY_PATH=. perl
When I type "use Animal;", I get the following error: "Can't locate loadable object for module Animal in @INC". I'm fairly new to perl so I'm not sure how to go about fixing the issue, although from searching around I feel like the problem might be that perl cannot reference my libmylib.so file. Any help would be greatly appreciated. Thank you!
The following seems to work on Ubuntu 16.04:
Animal.i:
%module Animal
%{
#include "Dog.h"
#include "Crow.h"
%}
%include "Dog.h"
%include "Crow.h"
Crow.h
class Crow {
public:
Crow() {
ncrows++;
}
virtual ~Crow() {
ncrows--;
}
static int ncrows;
};
Dog.h:
class Dog {
public:
Dog() {
ndogs++;
}
virtual ~Dog() {
ndogs--;
}
static int ndogs;
};
Crow.cpp:
#include "Crow.h"
int Crow::ncrows = 0;
Dog.cpp:
#include "Dog.h"
int Dog::ndogs = 0;
test.pl:
use strict;
use warnings;
use Animal;
print "Creating a Crow:\n";
my $c = Animal::Crow->new();
print " Created crow $c\n";
$c->DESTROY();
print "Creating a Dog:\n";
my $d = Animal::Dog->new();
print " Created dog $d\n";
$d->DESTROY();
swig -perl -c++ Animal.i
g++ -fPIC -c Crow.cpp
g++ -fPIC -c Dog.cpp
g++ -shared Crow.o Dog.o -o libmylib.so
g++ -fPIC -c Animal_wrap.cxx -I/usr/lib/x86_64-linux-gnu/perl/5.22/CORE
g++ -shared -L. Animal_wrap.o -lmylib -o Animal.so
$ LD_LIBRARY_PATH=. perl test.pl
Creating a Crow:
Created crow Animal::Crow=HASH(0x10c2eb0)
Creating a Dog:
Created dog Animal::Dog=HASH(0x10c2f88)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With