SWIG:仅使用标头和共享库为Perl包装C,无法找到可加载对象错误

前端之家收集整理的这篇文章主要介绍了SWIG:仅使用标头和共享库为Perl包装C,无法找到可加载对象错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试学习SWIG,我遇到一些问题,让SWIG与 Linux机器上的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

当我输入“use Animal;”时,我收到以下错误:“无法在@INC中找到模块Animal的可加载对象”.我对perl相当新,所以我不知道如何解决这个问题,虽然从搜索中我觉得问题可能是perl无法引用我的libmylib.so文件.任何帮助将不胜感激.谢谢!

解决方法

以下似乎适用于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)

猜你在找的Perl相关文章