我正在尝试学习SWIG,而且我在Linux机器上让SWIG与perl一起工作时遇到了一些问题。我有文件Dog.h,Crow.h,Animal.i和libmylib.so。所有这些文件都在同一目录中。 libmylib.so使用Dog.cpp和Crow.cpp编译,分别引用Dog.h和Crow.h。我的Animal.i文件如下:
%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"
以下是我为了构建perl模块而执行的命令:
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
当我输入"使用Animal;"时,我收到以下错误:"无法在@ INC"中找到模块Animal的可加载对象。我对perl相当新,所以我不确定如何解决这个问题,虽然从搜索中我觉得问题可能是perl无法引用我的libmylib.so文件。任何帮助将不胜感激。谢谢!
答案 0 :(得分:5)
以下似乎适用于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)